当前位置:  开发笔记 > 编程语言 > 正文

如何为这些按钮添加长按和手势?

如何解决《如何为这些按钮添加长按和手势?》经验,为你挑选了1个好方法。



1> Rashwan L..:

像这样做

@IBAction func playPauseAudioButton(sender: AnyObject) {
    let tapGesture = UITapGestureRecognizer(target: self, action: "normalTap:")
    let longGesture = UILongPressGestureRecognizer(target: self, action: "longTap:")
    tapGesture.numberOfTapsRequired = 1
    sender.addGestureRecognizer(tapGesture)
    sender.addGestureRecognizer(longGesture)
}

func normalTap(sender : UIGestureRecognizer) {
    let recognizer: UIGestureRecognizer = sender
    let tag: Int = recognizer.view!.tag

    switch tag {
    case 1:
        // Do some action for button 1
        print("1")
    case 2:
        print("2")
    case 3:
        print("3")
    case 4:
        print("4")
    case 5:
        print("5")
    case 6:
        print("6")
    default:
        print("Default")
    }
}

func longTap(sender : UIGestureRecognizer) {
    let recognizer: UIGestureRecognizer = sender
    let tag: Int = recognizer.view!.tag

    if sender.state == .Ended {
        print("UIGestureRecognizerStateEnded")
        //Do Whatever You want on End of Gesture
    }
    else if sender.state == .Began {
        print("UIGestureRecognizerStateBegan.")
        //Do Whatever You want on Began of Gesture
    }

    switch tag {
    case 1:
        // Do some action for button 1
        print("1")
    case 2:
        print("2")
    case 3:
        print("3")
    case 4:
        print("4")
    case 5:
        print("5")
    case 6:
        print("6")
    default:
        print("Default")
    }
}

推荐阅读
重庆制造漫画社
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有