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

时间:2015-02-26 18:32:52

标签: ios iphone swift uitapgesturerecognizer uianimation

我有一个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 个答案:

答案 0 :(得分:0)

  1. 您的dotTapped方法应该在animateButton方法之外。
  2. 确保userInteractionEnabled的{​​{1}}为真。