簡體   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