What we really want to do now is make the line animation cross into X. So let's get into our Didtaphamburgerbutton: method, we call it every time we click on this button and see what we're going to do.
- (void)didTapHamburgerButton:(id)sender { if (self.hamburgerOpen) { self.hamburgerOpenNO; // 添加把X变回三条线的动画 else { self.hamburgerOpenYES; // 添加把三条线变成X的动画 }}
We need a way to record whether the button is animated x (if it's a full app, that is, if the slide-out menu is rolled out), so I have a @property (BOOL) Hamburgeropen to the class, So that we can set it every time and know the current state of the button. This is the variable we should check before doing anything in this method, because its value indicates what type of animation we need to perform.
Let's start with the initial state, that is, Self.hamburgeropen is false, and the code executes from the else above. Before entering the actual code, let's discuss a plan to turn three horizontal lines into red X.
- We're going to rotate the top line down to a 45-degree angle.
- We're going to rotate the bottom line up a 45-degree angle.
- We don't need the middle line, so we just fade it out.
- The rotated lines may not cross well, so we want to animate them to the exact position
- Two intersecting lines from white animation to red
If you notice, you may realize that we have a lot of animations to do, and you're right. This is not an unimportant example, it consists of a number of individual animations, but like most animated code, it is executed step-by-step. We have been writing only one animation block at a time, except this time there are lots of animations! Let's start by fading out the middle row.
// 淡出中间行[UIView animateWithDuration:0.2 animations:^{ self.middle.alpha0.0f;}];
Just a simple block-based UIView animation. The goal of this fade-out animation is to make the middle line disappear, so we don't need to do anything else. Oh, I should mention that I have the top, middle, and bottom lines as @property of the class, which is why we can get this variable with the self. prefix.
Next, let's turn the saved two lines from the white animation into red. Fortunately, pop makes it very simple, you just need to set tovalue color for the color you want eventually, it will automatically insert the middle color.
//Change the color of the top and top lines to redPopspringanimation *topcolor = [ Self. Toppop_animationforkey:@"Topcolor"];if(Topcolor) {Topcolor. Tovalue= [UicolorRedcolor];}Else{Topcolor = [popspringanimation Animationwithpropertynamed:kpopviewbackgroundcolor]; Topcolor. Tovalue= [UicolorRedcolor]; Topcolor. Springbounciness=0; Topcolor. Springspeed=18.0F [ Self. TopPop_addanimation:topcolor forkey:@"Topcolor"];} Popspringanimation *bottomcolor = [ Self. Bottompop_animationforkey:@"Bottomcolor"];if(Bottomcolor) {Bottomcolor. Tovalue= [UicolorRedcolor];}Else{Bottomcolor = [popspringanimation Animationwithpropertynamed:kpopviewbackgroundcolor]; Bottomcolor. Tovalue= [UicolorRedcolor]; Bottomcolor. Springbounciness=0; Bottomcolor. Springspeed=18.0F [ Self. BottomPop_addanimation:bottomcolor forkey:@"Bottomcolor"];}
Just like our previous button example, when we repeat a user action, we need to make sure that our animations are flowing even when the user frantically clicks on the button and interrupts our animation. It is important to start the animation from the current value so that everything is natural. That's why I made a check that Topcolor and Bottomcolor animated objects already exist before creating and adding new animations. If they exist, we use the existing animation and set only a new tovalue, and if not, we build a new animated object. Also, I did not use any elasticity for this color transition, because I did not want the color animation to iterate over the red and then turn into some strange color.
When the user clicks on the button we don't have x yet, but we already have this lovely vision.
Finished viewing the integration set: Https://github.com/Cloudox/Motion-Design-for-iOS
All rights reserved: Http://blog.csdn.net/cloudox_
Motion Design for IOS (44)