是否可以使用UIGestureRecognizer捕获着陆和长按?

func gestureSetup() {
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapButton:")
    tapTempoButton.addGestureRecognizer(tapGestureRecognizer)


    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
    longPressRecognizer.minimumPressDuration = 1.0
    tapTempoButton.addGestureRecognizer(longPressRecognizer)
}

func tapButton(sender: UITapGestureRecognizer) {
   // On touch down update UI and play sound
}

最佳答案

示例:UITextLabel上的手势
//在标签上调用
让rotationGesture = UIRotationGestureRecognizer(target:self,action:“handleRotation:”)
rotationGesture.delegate =自我

    let tapToDeleteTextGesture = UITapGestureRecognizer(target: self, action: "tapToDeleteText:")
    tapToDeleteTextGesture.numberOfTapsRequired = 2

   textLabel?.addGestureRecognizer(rotationGesture)
    textLabel?.addGestureRecognizer(tapToDeleteTextGesture)


func handleRotation(recognizer: UIRotationGestureRecognizer){
    let state = recognizer.state
    if (state == UIGestureRecognizerState.Began || state == UIGestureRecognizerState.Changed){
        if let view = recognizer.view {
            view.transform = CGAffineTransformRotate(view.transform, recognizer.rotation)
            recognizer.rotation = 0
        }
    }
}

 func tapToDeleteText(recognizer: UITapGestureRecognizer){
    if let view = recognizer.view {
        view.removeFromSuperview()
    }
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

Add添加longPressGesture,您将被设置。

关于ios - Swift UIGestureRecognizer触地得分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33491361/

10-15 14:08