繁体   English   中英

如何在屏幕上设置动画时使UIImageView可以进行播放

[英]how to make a UIImageView tappable while it is animating across the screen

我有一个for循环,生成在屏幕上移动的点图像。 我不知道在哪里或如何实现一个点击手势识别器,它将应用于正在生成的所有点,并且当它们在移动/动画时被轻拍时能够使它们消失。 请帮帮我! 谢谢!

@IBAction func animateButton(sender: AnyObject) {

    self.view.userInteractionEnabled = true

    // Declare delay counter
    var delayCounter:Int = 100000
    var durationCounter:Double = 0

    // loop for 1000 times
    for loopNumber in 0...1000 {


        // set up some constants for the animations
        let dotDuration:Double = 4 - durationCounter
        let redDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
        let blueDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
        let yellowDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
        let greenDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000

        let options = UIViewAnimationOptions.AllowUserInteraction

        //set up some constants for the dots
        let redSize:CGFloat = 54
        let redYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54

        let blueSize:CGFloat = 54
        let blueYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54

        let yellowSize:CGFloat = 54
        let yellowYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54

        let greenSize:CGFloat = 54
        let greenYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54

        // create the dots and add them to the view
        let redDot = UIImageView()
        redDot.image = UIImage(named: "Red Dot")
        redDot.frame = CGRectMake(0-redSize, redYPosition, redSize, redSize)
        self.view.addSubview(redDot)

        let blueDot = UIImageView()
        blueDot.image = UIImage(named: "Blue Dot")
        blueDot.frame = CGRectMake(0-blueSize, blueYPosition, blueSize, blueSize)
        self.view.addSubview(blueDot)

        let yellowDot = UIImageView()
        yellowDot.image = UIImage(named: "Yellow Dot")
        yellowDot.frame = CGRectMake(0-yellowSize, yellowYPosition, yellowSize, yellowSize)
        self.view.addSubview(yellowDot)

        let greenDot = UIImageView()
        greenDot.image = UIImage(named: "Green Dot")
        greenDot.frame = CGRectMake(0-greenSize, greenYPosition, greenSize, greenSize)
        self.view.addSubview(greenDot)



        // define the animations
        UIView.animateWithDuration(dotDuration , delay: redDelay, options: options, animations: {
            redDot.frame = CGRectMake(675, redYPosition, redSize, redSize)
            }, completion: { animationFinished in redDot.removeFromSuperview() })

        UIView.animateWithDuration(dotDuration , delay: blueDelay, options: options, animations: {
            blueDot.frame = CGRectMake(675, blueYPosition, blueSize, blueSize)
            }, completion: { animationFinished in blueDot.removeFromSuperview() })

        UIView.animateWithDuration(dotDuration , delay: yellowDelay, options: options, animations: {
            yellowDot.frame = CGRectMake(675, yellowYPosition, yellowSize, yellowSize)
            }, completion: { animationFinished in yellowDot.removeFromSuperview() })

        UIView.animateWithDuration(dotDuration , delay: greenDelay, options: options, animations: {
            greenDot.frame = CGRectMake(675, greenYPosition, greenSize, greenSize)
            }, completion: { animationFinished in greenDot.removeFromSuperview() })

        // FAILED ATTEMPT
        var tapDot:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dotTapped")

        // FAILED ATTEMPT
        redDot.addGestureRecognizer(tapDot)

        // FAILED ATTEMPT
        func dotTapped (recognizer: UITapGestureRecognizer) {

            redDot.alpha = 0

        }







        durationCounter+=0.05
        delayCounter+=50000
    }


}
  1. dotTapped方法应该在animateButton方法之外。
  2. 确保UIImageViewuserInteractionEnabled为true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM