swift 选中长按项_Swift:Long Press手势识别器 - 检测水龙头和长按

I want to wire an action such that if the gesture is a tap, it does animates an object in a particular way but if the press duration was more than .5 secs it does something else.

Right now, I just have the animation hooked up. I don't know how I can differentiate between a long press and a tap?

How do I access the press duration to achieve the above?

@IBAction func tapOrHold(sender: AnyObject) {

UIView.animateKeyframesWithDuration(duration, delay: delay, options: options, animations: {

UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {

self.polyRotate.transform = CGAffineTransformMakeRotation(1/3 * CGFloat(M_PI * 2))

})

UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {

self.polyRotate.transform = CGAffineTransformMakeRotation(2/3 * CGFloat(M_PI * 2))

})

UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {

self.polyRotate.transform = CGAffineTransformMakeRotation(3/3 * CGFloat(M_PI * 2))

})

}, completion: { (Bool) in

let vc : AnyObject! = self.storyboard?.instantiateViewControllerWithIdentifier("NextView")

self.showViewController(vc as UIViewController, sender: vc)

})

解决方案

Define two IBActions and set one Gesture Recognizer to each of them. This way you can perform two different actions for each gesture.

You can set each Gesture Recognizer to different IBActions in the interface builder.

@IBAction func tapped(sender: UITapGestureRecognizer)

{

println("tapped")

//Your animation code.

}

@IBAction func longPressed(sender: UILongPressGestureRecognizer)

{

println("longpressed")

//Different code

}

Through code without interface builder

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")

self.view.addGestureRecognizer(tapGestureRecognizer)

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")

self.view.addGestureRecognizer(longPressRecognizer)

func tapped(sender: UITapGestureRecognizer)

{

println("tapped")

}

func longPressed(sender: UILongPressGestureRecognizer)

{

println("longpressed")

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值