【问题标题】:UITapGestureRecognizer does not work with UIView animateUITapGestureRecognizer 不适用于 UIView 动画
【发布时间】: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
        })
    }
}

【问题讨论】:

标签: ios swift animation uiview uitapgesturerecognizer


【解决方案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: {
    ...
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多