"IOS" Color Tabbar Toggle Animation Implementation

Source: Internet
Author: User

Inadvertently see a color tabbar switch of the design, feel very good, have time to bring him to realize.

Environmental information

Mac OS X 10.10.4

Xcode 6.4

IOS 8.4

:

Source:

Https://github.com/saitjr/STColorfulTabBar.git

Body

First, the realization analysis

When I saw this color switch effect, my first reaction was to have a color view on the Tabbar, and then the display of each color block was shown by mask. In the end, my specific implementation is based on this idea.

1. Design Ideas

In order to reduce the intrusion (coupling), I take the inheritance uitabbar to achieve, so in order to integrate, only need to change the system's Tabbar I write Tcolorfultabbar can be.

2. View Level diagram

View level diagram

Second, the effect achieved 1. Add a color view Colorfulview

Tcolorfultabbar.m

- (void) Setupcolorview {Initialize the color view and add it to the TabbarUIView *colorview = [[UIView Alloc] initWithFrame:Self.bounds]; [Self Addsubview:colorview];self.colorfulview = Colorview; //color view of five colors, this is an Uicolor array, nsarray *colors = self.itemcolors; cgfloat itemwidth = self.bounds.size.width/self.itemcount; for (int i = 0; i < self.itemcount; i + +) {uiview *view = [[uiview Alloc] Initwithframe:cgrectmake (Itemwidth * I, 0, Itemwidth,  Self.bounds.size.height)]; View.backgroundcolor = Colors[i]; [self.colorfulview Addsubview:view];} } 
2. Add matte Colorfulmaskview for color view

The reason for masking here is uiview instead of Cashapelayer or Calayer, for the following reasons:

    • Masks are rectangles, no need to use paths, so don't cashapelayer
    • As you can see from the design diagram, there is an effect of a matte one-way width (left or right) before the motion animation. If you use Calayer, then change the bounds, it is bidirectional change, want one-way must add anchor point, too troublesome
    • If the use of UIView, then the right to widen only need x unchanged, width is longer on the line, to the left to widen only need to x-value,width+value on the line, to be more convenient
    • The last point is when the animation, layer to use Cabasicanimation (cabaseanimation before the wrong, has been corrected), but UIView use UIView animation on the line

When setting the mask, set the layer of Colorfulmaskview to Colorfulview mask.

The code is implemented as follows:

Tcolorfultabbar.m

- (void) Setupmasklayer { //Gets the width of each item cgfloat itemwidth = self.bounds.size.width/ SELF.ITEMC Ount; //Initialize the Colormaskview and set the Colormaskview layer to the matte of the color view UIView *colormaskview = [[UIView alloc] initWithFrame :CGRectMake (0, 0, Itemwidth, self.bounds.size.height)]; colormaskview.backgroundcolor = [  Uicolor Blackcolor]; Self.colorfulmaskview = Colormaskview; self.colorfulView.layer.mask = self.colorfulMaskView.layer;}         
3. Set up the proxy to get the subscript of the current click

Uitabbar does not get the interface to click Subscript, but Uitabbardelegate has, this protocol is uitabbarcontroller by default. In order to reduce the coupling, the use of Uitabbar themselves to obtain subscript, so, it is necessary to uitabbar themselves to achieve their own delegate.

It is important to note that you cannot set self.delegate = self in the Uitabbar initialization method, because even if set, it will be overwritten by Uitabbarcontroller. The final solution is to modify the delegate to self when Uitabbar is loaded onto the parent view.

- (void)didMoveToSuperview {    [super didMoveToSuperview];    self.delegate = self;}

Because each move position and direction are related to the subscript of the previous subscript, it is necessary to use the attribute to record the two underlying values.

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {        NSInteger index = [self.items indexOfObject:item]; self.fromeIndex = self.toIndex; self.toIndex = index; // 拿到下标以后执行动画 [self animation];}
4. Move the matte to show different color blocks

When animating, you need to take into account the smoothness of the effect. The whole animation consists of two animations, one is the width of the animation, one is reduced to the original size and displacement animation. The whole animation is a easeinout effect, so take a look, it should be the first animation Easein, the second animation easeout.

A picture wins thousands of words:

Animated linear graphs

- (void) Animation {CGFloat itemwidth =Self.bounds.size.width/Self.itemcount;To make the effect look smoother, calculate the distance that needs to be magnified based on the difference of two sub-valuesCGFloat extrawidth = ABS (Self.toindex-SELF.FROMEINDEX) * Itemwidth/4;Zoom in sizeCGRect Scaleframe =CGRectMake (Self.colorfulmaskview.x,0, Itemwidth + extrawidth,Self.bounds.size.height);The final big size and positionCGRect Toframe =CGRectMake (Self.toindex * Itemwidth,0, Itemwidth,Self.bounds.size.height);If you are moving to the left, change the coordinates of the x-axisif (Self.fromeindex >Self.toindex) {Scaleframe =CGRectMake (Self.colorfulmaskview.x-extrawidth,0, Itemwidth + extrawidth,self.bounds.size.height); } //Two animations add up to be a easeinout effect, so the first animation should be Easein, the second animation is easeout //Zoom in [UIView animatewithduration: Animation_duration delay:0 Options:uiviewanimationoptioncurveeasein animations:^{  Self.colorfulMaskView.frame = Scaleframe; } completion:^ (BOOL finished) { //re-scaling and displacement [UIView animatewithduration:animation_duration delay:0 Options:uiviewanimationoptioncurveeaseout animations:^{ self.colorfulMaskView.frame = toframe;} Completion:NULL]; }];}

"IOS" Color Tabbar Toggle Animation Implementation

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.