UILongPressGestureRecognizer-按住

时间:2017-03-15 21:30:58

标签: ios swift swift3 uikit gesture-recognition

我正在尝试制作一个功能,当用户手指向下时,图像会在转换发生时一直增长到一定大小。我知道如何使图像成长,但不知道如何在用户按下时使其成长。

有谁知道如何让这种情况发生?是否需要LongPressRecognizer?

很抱歉没有代码,但我只想弄清楚如何做到这一点。非常感谢您的帮助!

干杯, 西奥

1 个答案:

答案 0 :(得分:2)

你可以这样做:

@IBOutlet weak var myImageView: UIImageView!

var startAnimation = false
func handleTap(gestureRecognizer:UIGestureRecognizer) {

        startAnimation = true
        UIView.animate(withDuration: 0.5, animations: {
            self.myImageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
            self.view.layoutIfNeeded()
        }) { (finished) in
            self.startAnimation = false
        }
}

override func viewDidLoad() {
    super.viewDidLoad()

    let gesture = UILongPressGestureRecognizer(target: self, action: #selector(handleTap(gestureRecognizer:)))

    myImageView.isUserInteractionEnabled = true
    myImageView.addGestureRecognizer(gesture)

}