我想连接一个动作,如果这个手势是一个点击,它确实会以一种特定的方式为一个对象设置动画,但如果按下的持续时间超过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.这样,你可以 for each 手势执行两个不同的动作.

You can set each 100 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")
}

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

Swift相关问答推荐

通过withstickedContinuation传递通用类型T

为表单部分赋予背景 colored颜色 /渐变

向文本元素添加背景色失败

仅当单击UIButton时才调用计时器函数

解码器是否需要具有密钥的外部数据表示?

使用序列初始化字符串的时间复杂度是多少?

Xcode 15 Beta中如何使用@Observable?

macOS 的窗口框架中使用的真实类型和真实类型是什么?

如何制作进度加载动画?

在 struct 中的序列和非序列泛型函数之间进行 Select

为什么 Apple Scrumdinger Sample App 使用两个事实来源?

符合协议 - 错误?

在 Swift 中实现自定义异步序列

如何将使用\u{ea12}创建的 Swift 字符串转换回其十六进制值/字符串ea12?

Apple 的自然语言 API 返回意外结果

临时添加到视图层次 struct 后,Weak view引用不会被释放

无法推断泛型参数的参数

WKWebView 确实从本地文档文件夹加载资源

枚举大小写的原始值必须是文字

UITableView 布局在 push segue 和 return 上搞砸了. (iOS 8、Xcode beta 5、Swift)