[英]UITapGestureRecognizer does not work with UIView animate
當我在動畫播放時點擊 UIImage 時,我試圖調用一個動作。 我見過類似的問題,但我無法將這些解決方案應用於我的案例。 請幫忙。
Xcode 9.2 快速 4
import UIKit
class MyCalssViewController: UIViewController {
@IBOutlet weak var myImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// action by tap
let gestureSwift2AndHigher = UITapGestureRecognizer(target: self, action: #selector (self.actionUITapGestureRecognizer))
myImageView.addGestureRecognizer(gestureSwift2AndHigher)
}
// action by tap
@objc func actionUITapGestureRecognizer (){
print("actionUITapGestureRecognizer - works!") // !!! THIS DOES NOT WORK !!!
}
// hide UIImage before appear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
myImageView.center.y += view.bounds.height
}
// show UIImage after appear with animation
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 10, animations: {
self.myImageView.center.y -= self.view.bounds.height
})
}
}
您只是錯過了一行,從而啟用了視圖上的用戶交互。
override func viewDidLoad() {
super.viewDidLoad()
// action by tap
let gestureSwift2AndHigher = UITapGestureRecognizer(target: self, action: #selector (self.actionUITapGestureRecognizer))
myImageView.isUserInteractionEnabled = true
myImageView.addGestureRecognizer(gestureSwift2AndHigher)
}
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.clickedImageView(_:)))
myImageview.isUserInteractionEnabled = true
myImageview.addGestureRecognizer(tapGesture)
// MARK: - Gesture Recognizer action
func clickedImageView(_ sender: UITapGestureRecognizer) {
// Write your action here.
}
您需要像這樣傳入選項.allowUserInteraction
:
UIView.animate(withDuration: 10, delay: 0, options: .allowUserInteraction, animations: {
...
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.