Let's take a look at what we're going to achieve today, and the effect achieved today with the first view animations can do the same thing.
Animated by the book "IOS Animations by Tutorials", I'm just a narrator
Well, let's take a look at what layer is:
More popular, Calayer is the uiview of the view layer, you see the UIView, is actually uiview layer. So, Calayer is the leaf chlorophyll, and the chlorophyll is different, calayer more "simple", we know that the chlorophyll is a lot of matrix, and Calayer just represents everything you can see.
Today we put all the emphasis on the animation of the writing, the default has the Swift Foundation If you observe carefully, you will find that the background of the cloud is gradual, that is, the transparency from 0 to 1, of course, the uiviewanimation we learned in front of it is very easy to achieve, So how do you do it with Calayer, look at the following code:
//1 Let fadeIn = Cabasicanimation (keypath: "opacity")//2 fadein.fromvalue = 0.0//3 fadeIn. Tovalue = 1.0//4 fadein.duration = 0.5//5 Fadein.fillmode = kcafillmodebackwards//6 fadein.begintime = Cacurrentmediatime () + 0.5 cloud1.layer.addAnimation (fadeIn, forkey:nil) Fadein.begintime = Cacurrentmediatime () + 0.7 cloud2.layer.addAnimation (fadeIn, forkey:nil) Fadein.begintime = cacurrentmediatime () + 0.9 cloud3.layer.a Ddanimation (FadeIn, forkey:nil) Fadein.begintime = cacurrentmediatime () + 1.1 cloud4.layer.addAnimation (FadeIn, for Key:nil)
It's obvious that we added animations to the four-cloud layer , and then we realized the fade-in effect.
- 1, this sentence declares a cabasicanimation, notice inside fill in the parameter not, fill is opacity, is the meaning of transparency, here head also can fill many other values, for example position, Of course, we'll talk about this later.
- 2, we set the initial value of the animation, that is, the first transparency is 0.
- 3, we set the final value of the animation, that is, the transparency of 1.
- 4, animation duration, we set to 0.5 seconds. In combination with the previous three sentences, the meaning is expressed: the animation changes the transparency of the object, and in 0.5 seconds, the transparency changes from 0 to 1.
- 5, we give Fillmode attribute value is kcafillmodebackwards, then kcafillmodebackwards this value is what use, this attribute can display object frame, we can put this sentence comment later run the program to see the effect , we will find that before the animation, the cloud is still visible, and this is obviously a bug, how to solve this bug, in fact, there are many ways, such as we can say that the cloud is set to 0 transparency, and then calculate the animation time, when the animation is finished, the transparency of the cloud is set to 1, This can certainly achieve the same effect, but it is really not so elegant, but also a way to add the Fillmode property, kcafillmodebackwards means to show the initial state of the animation, while there are two other values Kcafillmodeforwards can show the effect of the object after animation,Kcafillmodeboth is to take into account the above two effects.
- 6, this property is very good explanation, each cloud animation is not at the same time, then we give the cloud set the start time, this property and we said earlier Uiviewanimation delay this parameter is similar.
The above content realizes the gradual animation of the clouds.
If I have already said several times uiviewaniamtion have doubts, please read my previous article, feel good words please pay attention to me, then point a like it, Pro ~ ~
The next implementation is the title,Username,PassWord has a screen outside the left-to-right motion to the center of the animation, directly on the code:
//1 let flyRight = CABasicAnimation(keyPath: "position.x") flyRight.toValue = view.bounds.size.width/2 flyRight.fromValue = -view.bounds.size.width/2 flyRight.duration = 0.5 heading.layer.addAnimation(flyRight, forKey: nil) //2 flyRight.beginTime = CACurrentMediaTime() + 0.3 flyRight.fillMode = kCAFillModeBackwards username.layer.addAnimation(flyRight, forKey: nil) flyRight.beginTime = CACurrentMediaTime() + 0.4 password.layer.addAnimation(flyRight, forKey: nil)
- 1 through the Cloud animation, I believe we have been able to roughly understand this piece of code, and the only difference is that we created here Cabasicanimation animation object is "position.x",Fromvalue and Tovaule believe also do not have too much to explain, it is worth a question is our value refers to the object's center.x, not the upper left corner.
- 2 for username delay of 0.3 seconds. Also set Flyright.fillmode = Kcafillmodebackwards
Isn't it simple, yes ~
Log in button animation, on the code:
UIView.animateWithDuration(0.5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: nil, animations: { self.loginButton.center.y -= 30.0 self.loginButton.alpha = 1.0 }, completion: nil)
The explanation for this piece of code here, said very detailed, the author is also very handsome, look at the avatar can be seen: http://www.jianshu.com/p/bd7bf438b288 like, please pay attention to him.
We also find that the clouds are constantly moving and continue on the code:
func animateCloud(cloud: UIImageView) { let cloudSpeed = 60.0 / view.frame.size.width let duration = (view.frame.size.width - cloud.frame.origin.x) * cloudSpeed UIView.animateWithDuration(NSTimeInterval(duration), delay: 0.0, options: .CurveLinear, animations: { cloud.frame.origin.x = self.view.frame.size.width }, completion: {_ in cloud.frame.origin.x = -cloud.frame.size.width self.animateCloud(cloud) }) }
> Explanation Please refer to the Log in button for explanation. -------------first left only click **log in** button after the animation, we first look at what happened, when we click on the button, the button duang~ jumped down to the bottom, at the same time the color changed, the fillet became larger, and then added an activity indicator. On the code:
@IBAction func login() { //1 UIView.animateWithDuration(1.5, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: nil, animations: { self.loginButton.bounds.size.width += 80.0 }, completion: nil) //2 UIView.animateWithDuration(0.33, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: nil, animations: { self.loginButton.center.y += 60.0 //3 self.spinner.center = CGPoint(x: 40.0, y: self.loginButton.frame.size.height/2) self.spinner.alpha = 1.0 }, completion: {_ in //4 self.showMessage(index: 0) }) //5 let tintColor = UIColor(red: 0.85, green: 0.83, blue: 0.45, alpha: 1.0) tintBackgroundColor(layer: loginButton.layer, toColor: tintColor) //6 roundCorners(layer: loginButton.layer, toRadius: 25.0) }
This paragraph is very good, because this section is very long.
- 1, the duang~ button has become longer.
- 2, the duang~ button moves down.
- 3, add the activity indicator.
- 4, add message after this.
- 5, call the Tintbackgroundcolor method, change the color, this is the code for the Tintbackgroundcolor method:
func Tintbackgroundcolor (#layer: Calayer, #toColor: uicolor) {Let tint = cabasicanimation (keypath: "BackgroundColor") Tint.fromvalue = Layer.backgroundcolor Layer.backgroundcolor = Tocolor.cgcolor Tint.tovalue = ToColor.CGColor Tint.duration = 1.0 Tint.fillmode = Kcafillmodeboth layer.addanimation (tint, Forkey:nil)}
In fact, this method and the previous cabasicgroundcolor is basically the same, we first change the color into the color we need to become, and then perform the animation.
- 6. Animation with rounded corners
func roundcorners (#layer: Calayer, #toRadius: cgfloat) {Let round = cabasicanimation (KEYP Ath: "Cornerradius") Round.fromvalue = Layer.cornerradius Layer.cornerradius = Toradius Round.tovalue = ToRadius Round.du ration = 0.33 layer.addanimation (round, Forkey:nil)}
This implementation method is basically the same as the idea of changing the color above. You can also change the fillet first, then perform the animation, and the final display will be the rounded corners you set at the beginning.
Now the whole animation is left with the message animation, and after the end of the animation of the message, the Log in button bounces back the animation, and the log in button bounces back to the same animation as the button just mentioned, just the opposite. Let's take a look at the animation of the message :
Func removemessage (#index: Int) {uiview.animatewithduration (0.33, delay:0.0, Options:nil, animations: {Self.st Atus.center.x + = Self.view.frame.size.width}, Completion: {_ in Self.status.hidden = True Self.status.center = Self.statusposition self.showmessage (index:index+1)})} func Resetform () {Uiview.transitionwithview (STA Tus, duration:0.2, options:. Transitionflipfromtop, animations: {Self.status.hidden = True self.status.center = Self.statusposition}, CO Mpletion:nil) uiview.animatewithduration (0.2, delay:0.0, Options:nil, animations: {self.spinner.center = Cgpoi NT (x: -20.0, y:16.0) Self.spinner.alpha = 0.0 self.loginButton.bounds.size.width-= 80.0 Self.loginbutton. CENTER.Y-= 60.0}, Completion: {_ in Let Tintcolor = Uicolor (red:0.63, green:0.84, blue:0.35, alpha:1.0) Tintbackgroundcolor (Layer:self.loginButton.layer, Tocolor:tintcolor) roundcorners (layer:self.loginButton.laYer, toradius:10.0)})}
iOS animation--layer animations