gpt4 book ai didi

ios - Swift: Long Press Gesture Recognizer - 检测点击和长按

转载 作者:IT王子 更新时间:2023-10-29 05:03:10 32 4
gpt4 key购买 nike

我想连接一个 Action ,如果手势是点击,它会以特定方式为对象设置动画,但如果按下持续时间超过 0.5 秒,它会执行其他操作。

现在,我刚刚连接了动画。我不知道如何区分长按和点击?我如何访问新闻持续时间以实现上述目标?

 @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)
})

最佳答案

定义两个 IBActions 并为每个设置一个 Gesture Recognizer。这样您就可以为每个手势执行两个不同的操作。

您可以在界面构建器中将每个Gesture Recognizer 设置为不同的 IBAction。

@IBAction func tapped(sender: UITapGestureRecognizer)
{
println("tapped")
//Your animation code.
}

@IBAction func longPressed(sender: UILongPressGestureRecognizer)
{
println("longpressed")
//Different code
}

通过没有界面生成器的代码

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")
}

swift 5

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped))
self.view.addGestureRecognizer(tapGestureRecognizer)

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed))
self.view.addGestureRecognizer(longPressRecognizer)

@objc func tapped(sender: UITapGestureRecognizer){
print("tapped")
}

@objc func longPressed(sender: UILongPressGestureRecognizer) {
print("longpressed")
}

关于ios - Swift: Long Press Gesture Recognizer - 检测点击和长按,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28058082/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com