событие касания не срабатывает при просмотре анимированных изображений - PullRequest
0 голосов
/ 02 мая 2019

Я хочу, чтобы анимация imageView с UIBezierPath (скользящая сверху вниз) была нажата.Но каждая помощь Google, которую я нашел, не решала ее.Но он не печатает «нажал».

Анимация работает отлично.Может быть, у кого-нибудь есть подсказка?

Помощь очень ценится.

override func viewDidLoad() {
    super.viewDidLoad()

    let imageView = UIImageView(image: UIImage(named: "advent_button"))
    let dimension = 25 + drand48() * 30
    imageView.frame = CGRect(x: 0, y: 0, width: dimension, height: dimension)

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleImageTap))
    imageView.addGestureRecognizer(tapGestureRecognizer)
    imageView.isUserInteractionEnabled = true

    let animation = CAKeyframeAnimation(keyPath: "position")
    animation.path = customPath().cgPath
    animation.duration = 20
    animation.fillMode = .forwards
    animation.isRemovedOnCompletion = false
    animation.repeatCount = 0.5
    animation.timingFunction = CAMediaTimingFunction(name: .easeOut)

    imageView.layer.add(animation, forKey: nil)
    view.addSubview(imageView)        
}

@objc func handleImageTap() {
    print ("clicked")
}

func customPath() -> UIBezierPath {
    let path = UIBezierPath()

    path.move(to: CGPoint(x: 200, y: 0))
    let endpoint = CGPoint(x: 20 , y: 700)
    let cp1 = CGPoint(x: 90, y: 250)
    let cp2 = CGPoint(x: 180, y: 400)
    path.addCurve(to: endpoint, controlPoint1: cp1, controlPoint2: cp2)
    return path
}

Ответы [ 2 ]

0 голосов
/ 03 мая 2019

Я пробовал следующее:

UIView.animate(withDuration: 30, delay: delay, options: [.allowUserInteraction], animations: {
        self.button.frame = CGRect(
            x: randomXEndShift,
            y: 700,
            width: dimension,
            height: dimension)
    }, completion: nil)

Показывает то же поведение. Даже использование .allowUserInteraction совсем не помогает.

0 голосов
/ 02 мая 2019

Я получил вашу проблему.Я уверен, что вы касаетесь слоя не UIImageView, и вы добавили uitapgesturerecognizer к UIImageview не слоя.Выполните одно действие, установите

animation.isRemovedOnCompletion = true

и установите imageView.frame аналогичные координаты, где анимация остановится

Полный код:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let imageView = UIImageView(image: UIImage(named: "Icon123"))
        imageView.backgroundColor = .red
      //  let dimension = 25 + drand48() * 30

        imageView.isUserInteractionEnabled = true

        imageView.frame = CGRect(x: 10, y: 200, width: 50, height: 50)

        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleImageTap))
        imageView.addGestureRecognizer(tapGestureRecognizer)

        let animation = CAKeyframeAnimation(keyPath: "position")
        animation.path = customPath().cgPath
        animation.duration = 5
        animation.fillMode = .forwards
        animation.isRemovedOnCompletion = true
        animation.repeatCount = 0.5
        animation.timingFunction = CAMediaTimingFunction(name: .easeOut)

        imageView.layer.add(animation, forKey: nil)
        view.addSubview(imageView)


        // Do any additional setup after loading the view, typically from a nib.
    }


    @objc func handleImageTap() {
        print ("clicked")
    }

    func customPath() -> UIBezierPath {
        let path = UIBezierPath()

        path.move(to: CGPoint(x: 200, y: 0))
        let endpoint = CGPoint(x: 20 , y: 700)
        let cp1 = CGPoint(x: 90, y: 250)
        let cp2 = CGPoint(x: 180, y: 400)
        path.addCurve(to: endpoint, controlPoint1: cp1, controlPoint2: cp2)
        return path
    }

}
...