简体   繁体   中英

Swift Animation Enclosure Won't Animate

I am creating an iPad application in Swift that doesn't use AutoLayout. My viewDidLoad method is empty, and I placed my animation code inside of my IBAction, as shown below:

@IBAction func nextStep(sender: AnyObject) {
        UIView.animateWithDuration(1, animations: { () -> Void in
            self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x - 400, self.comparisonLabel.center.y)
        })
        comparisonLabel.text = "Speed Reading"
        comparisonLabel.center = CGPointMake(comparisonLabel.center.x + 800, comparisonLabel.center.y)
        UIView.animateWithDuration(1, animations: { () -> Void in
            self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x - 400, self.comparisonLabel.center.y)
        })
}

The first UIView enclosure does not animate the label. Rather, it just instantly removes the label from the screen. For some reason, the second UIView works exactly as it should, gradually sliding the label from the right side of the screen. (I didn't use the viewDidAppear function because I only need the animations to take place when the user clicks a button.)

Would anyone happen to know why this is happening, and how I can make the first UIView animation perform the same as the second?

Thanks in advance to all who reply.

Use UIView.animateKeyframesWithDuration instead.

Example

UIView.animateKeyframesWithDuration(2, delay: 0, options: nil, animations: { () -> Void in

        UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.5, animations: {

            //Move left animation
            self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x - 400, self.comparisonLabel.center.y)


        })

        UIView.addKeyframeWithRelativeStartTime(0.5, relativeDuration: 0.1, animations: {

            //Hiding animation
            self.comparisonLabel.alpha = 0

        })

        UIView.addKeyframeWithRelativeStartTime(0.6, relativeDuration: 0.1, animations: {

            //Move to right animation
            self.comparisonLabel.text = "Speed Reading"
            self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x + 800, self.comparisonLabel.center.y)

        })

        UIView.addKeyframeWithRelativeStartTime(0.6, relativeDuration: 0.4, animations: {

            //Moving back into screen animation
            self.comparisonLabel.alpha = 1
            self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x - 400, self.comparisonLabel.center.y)

        })

        }, completion: nil)

Try to play with relativestarttime and relativeduration to get your desire effect.

Or use

class func animateWithDuration(_ duration: NSTimeInterval,
                animations animations: () -> Void,
                completion completion: ((Bool) -> Void)?)

Example

UIView.animateWithDuration(1, animations: { () -> Void in

        //Your first animation
        self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x - 400, self.comparisonLabel.center.y)

        }) { (finish:Bool) -> Void in

            self.comparisonLabel.text = "Speed Reading"
            self.comparisonLabel.center = CGPointMake(comparisonLabel.center.x + 800, comparisonLabel.center.y)

            UIView.animateWithDuration(1, animations: { () -> Void in
                //Your second animation
                self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x - 400, self.comparisonLabel.center.y)
            })
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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