An analysis of the integrated _ios of IOS Multi-view Sliding click Switching

Source: Internet
Author: User

Objective

Multi-View Sliding Click Switch This view is useful in many apps, I encapsulate this view, the outside only need to call an interface, can achieve this effect, use method and system Tabbarcontroller very similar.

The outside world only needs to call the following interface to integrate.

/**
 * Add a child controller *
 /
(void) Addsubitemwithviewcontroller: (Uiviewcontroller *) Viewcontroller;

Hytabbarview effect chart is as follows

Hytabbarview can be flexibly configured with a screen wide display how many titles, and the height of the title bar, specific to see the project needs

#define HYTABBARVIEWHEIGHT 49//Top tab Bar height
#define HYCOLUMN 4  //One screen width Display 4 titles

Implementation of the idea of detailed

1, Interface analysis: divided into the upper and lower parts, the top UIScrollView , the bottom UICollectionView . Then realize the two-part linkage can be realized (the bottom view is relatively complex, occupy large memory, the bottom UICollectionView of the implementation will be much better than the UIScrollView performance)

2, each title corresponds to a view, view views to the appropriate controller to manage, the structure of the code is very clear. To achieve high aggregation of business logic on different view. And it doesn't have to be coupled.

3, the upper and lower two parts of the linkage, here is the same KVO implementation, listening to the current selectedIndex , the bottom view scrolling, the modified selectedIndex value. Center the caption in the callback method that Kvo listens to.

4, other details relatively simple, we do not look at the code know how to deal with, such as: Click on the top of the title, set the button selected, switch to the corresponding CollectionCell

UI Structure Sketch

Code fragment:

1. The outside to pass a controller and a title, add a column

The outside passes a controller, adds a column
-(void) Addsubitemwithviewcontroller: (Uiviewcontroller *) viewcontroller{ 
  
 UIButton * btn = [ UIButton Buttonwithtype:uibuttontypecustom];
 [Self.tabbar addsubview:btn];
 [Self setupbtn:btn withTitle:viewController.title];
 [Btn addtarget:self Action: @selector (itemselected:) forcontrolevents:uicontroleventtouchupinside];
 [Self.subviewcontrollers Addobject:viewcontroller];
}

2.KVO listens for the ordinal value of the currently selected view

Add observer in viewdidload [self addobserver:self forkeypath:@ "SelectedIndex" Options:nskeyvalueobservingoptionold |
 
Nskeyvalueobservingoptionnew context:@ "Scrolltonextitem"]; Let the caption button center algorithm-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (Nsdictionary *) change Context: (void *) context{if (context = = @ "Scrolltonextitem") {self.prevselectedindex = [change[@ ' old '] Integervalu
  E];
  if (Self.prevselectedindex = = Self.selectedindex) {return;
  //Set button to select [Self ItemSelectedIndex:self.selectedIndex];
  
  UIButton * btn = Self.titles[self.selectedindex];
  Let the selected button be centered nsinteger min = HYCOLUMN/2;
   if (_selectedindex = self.titles.count-min) {UIButton * tempbtn = self.titles[self.titles.count-min-1]; CGFloat btnx = (hycolumn% 2)?
   Tempbtn.center.x: (tempbtn.center.x + btn.frame.size.width * 0.5);
   CGFloat OffsetX = _tabbar.center.x-btnx; [UIView animatewithduration:0.25 animations:^{_tabbar.contentoffset = cgpointmake (-offsEtX, 0);  
  }];  }else if (_selectedindex > Min && _selectedindex < self.titles.count-min && Self.titles.count >
   Hycolumn) {CGFloat btnx = (hycolumn% 2)? btn.center.x: (Btn.center.x-btn.frame.size.width * 0.5);
   CGFloat OffsetX = _tabbar.center.x-btnx;
   [UIView animatewithduration:0.25 animations:^{_tabbar.contentoffset = Cgpointmake (-offsetX, 0);
  }];
 } else {[Super Observevalueforkeypath:keypath ofobject:object Change:change Context:context];  }
}

The controller code is as follows

Using a method similar to a system UITabbarController , the outside world only needs direct incoming controllers.

-(void) viewdidload {
 [super viewdidload];
 [Self.view AddSubview:self.tabbarView];
} Lazy Load-(Hytabbarview *) tabbarview{if (!_tabbarview) {
  _tabbarview = ({
   Hytabbarview * tabbar = [[Hytabbarview All Oc]initwithframe:cgrectmake (0,30,[uiscreen mainscreen].bounds.size.width,600)];   
   for (Nsinteger i = 0; i< i + +) {    
    Uiviewcontroller * vc = [[Uiviewcontroller alloc]init];
    Vc.title = [NSString stringwithformat:@ "%ld", i+1];
    [Tabbar ADDSUBITEMWITHVIEWCONTROLLER:VC];
   }
   Tabbar;
  });
 return _tabbarview;
}

Summarize

These are the full contents of the iOS multi-view slide-click Switch, and I hope it helps when developing iOS.

Related Article

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.