UITapGestureRecognizer 不适用于 UIView 动画
Posted
技术标签:
【中文标题】UITapGestureRecognizer 不适用于 UIView 动画【英文标题】:UITapGestureRecognizer does not work with UIView animate 【发布时间】:2017-12-27 20:24:09 【问题描述】:当我在 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
)
【问题讨论】:
查看***.com/questions/37818440/… “我见过类似的问题,但我无法应用这些解决方案”这些解决方案是什么? 【参考方案1】:您只是漏掉了一行,在视图上启用了用户交互。
override func viewDidLoad()
super.viewDidLoad()
// action by tap
let gestureSwift2AndHigher = UITapGestureRecognizer(target: self, action: #selector (self.actionUITapGestureRecognizer))
myImageView.isUserInteractionEnabled = true
myImageView.addGestureRecognizer(gestureSwift2AndHigher)
【讨论】:
thnx,但这行没有帮助【参考方案2】: 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.
【讨论】:
这部分“_发件人:UITapGestureRecognizer”没有任何改变 发件人将通过特定的 UITapGestureRecognizer 进行验证 我明白,但是当 ImageView 动画时,点击操作仍然不起作用 可以分享动画功能吗 请看高一点,我已经在我的问题中做到了" // 在出现之前隐藏 UIImage override func viewWillAppear(_ animated: Bool) super.viewWillAppear(animated) myImageView.center.y += view.bounds.height // 显示 UIImage 后出现动画覆盖 func viewDidAppear(_ animated: Bool) super.viewDidAppear(animated) UIView.animate(withDuration: 10, animations: self.myImageView.center.y -= self.view.bounds.height ) "【参考方案3】:你需要像这样传入选项.allowUserInteraction
:
UIView.animate(withDuration: 10, delay: 0, options: .allowUserInteraction, animations:
...
)
【讨论】:
以上是关于UITapGestureRecognizer 不适用于 UIView 动画的主要内容,如果未能解决你的问题,请参考以下文章
UITapGestureRecognizer 不适用于 UIImageView 自定义类
UITapGestureRecognizer 不适用于 UITextField 的自定义子类
UITapGestureRecognizer 不适用于 UIView 动画
UITapGestureRecognizer 适用于 UIImageView 但不适用于 UILabel