Motion Design for IOS (32)

Source: Internet
Author: User

Now let's add our icon button.

//Add iconUIButton*icon = [UIButtonButtonwithtype:uibuttontypecustom]; [Icon setimage:[UIImageimagenamed:@"Map-icon"] Forstate:uicontrolstatenormal]; [Icon AddTarget: SelfAction@selector(Didtapmapicon:) forcontrolevents:uicontroleventtouchupinside]; [Icon Setframe:cgrectmake ( Self. Window. Bounds. Size. Width- the, +, the, -)]; [ Self. WindowAddsubview:icon];

This is a very typical way to add icon buttons. UIButtonclass has a convenient way to construct a button: +buttonwithtype: Class method. I set the button type to UIButtonTypeCustom , which means that no default style will be set, which is entirely up to me. This is a useful simple icon button, without Borders and other weird styles that need to be removed. A bit similar to resetting the buttons in CSS.

Next I set the picture of the button to the "Map-icon" image in my app package. Parameter UIControlStateNormal means that this icon will be displayed as a picture of the button in the general, default state. You can set this value multiple times with multiple images, as long as you want to change the icon, such as UIControlStateHighlighted status. By default, when one UIButton is clicked, iOS automatically dims the picture.

Finally, I let the button be clicked and invoke a method I defined. The self parameter value means I want this button to call the class where it was clicked, and the @selector(didTapMapIcon:) objective-c method I want to invoke. Next I put the button in the exact position by setting the frame.

Let's look at what the app looks like and the alpha value of the map is set to 0, so it's not visible.

OK, now that we've added all the major parts of the animation to the interface, it's time to add some animations when the map icon is clicked.

First, we need to implement the method that is called when the button is clicked. Here is what the method looks like without any content.

- (void)didTapMapIcon:(id)sender {    // 暂时没有任何内容!}

It will be called when the user taps the map button because we have previously set it through the-addtarget:action:forcontrolevents: method.

So, by logic, when you click the button, one of the following two events occurs: Animate the map to the interface, or animate the map out of the interface if the map is already visible. We can examine our interface elements and look at their location to determine what we should do, but that's too much trouble, so let's keep track of the state through a simple BOOL variable that acts as a class @property. At the top of this file I added a bool variable named mapshowing to manage whether we need to open or close the map view. This property is placed below the method of our button, and the other properties we add are the main view of our interface.

@interface DTCAppDelegate ()- (void)didTapMapIcon:(id)sender;@property (assignBOOL mapShowing;@property (strongUIImageView *appBackground;@property (strongUIImageView *mapView;@end

Now, back to our button click Method, we need to add some logic here to check whether the map is displayed or not, and then set the variable to the opposite.

- (void)didTapMapIcon:(id)sender {if (self.mapShowing) {      self.mapShowingNO;    // 当地图已经可见时要运行的代码else {    self.mapShowingYES;    // 当地图不可见时要运行的代码}

Let else 's start with the situation where the map is not showing and we need to animate the opacity. What we need to do is fade out the main app background a little bit and fade in the map. The main app background fades faster than the map fades, so the map will be more visible.

[uiview  Animatewithduration:.5  delay:0  options: Uiviewanimationoptioncurveeaseinout| Uiviewanimationoptionbeginfromcurrentstate animations:^{self  .appbackground  .alpha  = 0.3  F;} Completion:null ]; [uiview  animatewithduration:.15  Delay:0  options:uiviewanimationoptioncurveeaseinout| Uiviewanimationoptionbeginfromcurrentstate animations:^{self  .mapview  .alpha  = 1.0  F;} Completion:null ];  

You may have noticed the huge parameters placed in the general options basis of this block-based UIView animation method. This is actually two options via binary | The actions are grouped together: the UIViewAnimationOptionCurveEaseInOut fade that defines the animation UIViewAnimationOptionBeginFromCurrentState starts with its alpha current value, so that even if the animation is interrupted, it does not jump back to the initial value before the animation starts. This is important for animations like this being managed by user actions, because you don't know if the user will be able to click the button after the animation has occurred, and you don't want to do anything after the animation is done.

Of course, adjusting the opacity of the main app interface and the map does not exactly complete our animation, because we also need the scale and position of the animated map so that it can reach its final position and size. For the main app interface, we will only slightly animate its proportions.

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 (32)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.