关于Tab控件 iOS


用代码来控制tab切换:

I'm switching tabs programmatically in a tab bar driven application using UITabBarController.selectedIndex. The problem I'm trying to solve is how to animate the transition between the views. ie.. from the view of the current tab to the view of the selected tab.

The first thought was to make use of the UITabBarControllerDelegate, but it appears that this is not called when programmatically switching tabs. I'm now considering the UITabBarDelegate.didSelectItem: as a possible hook to set a transition animation. 动画效果来切换:

Solution 1: transition from view (simple)

This is the easiest and makes use of a predefined UIView transition method. With this solution we don't need to manage the views because the method does the work for us.

// Get views. controllerIndex is passed in as the controller we want to go to.  UIView * fromView = tabBarController.selectedViewController.view; UIView * toView = [[tabBarController.viewControllers objectAtIndex:controllerIndex] view]; // Transition using a page curl. [UIView transitionFromView:fromView                     toView:toView                   duration:0.5                    options:(controllerIndex > tabBarController.selectedIndex ? UIViewAnimationOptionTransitionCurlUp : UIViewAnimationOptionTransitionCurlDown)                 completion:^(BOOL finished) {                     if (finished) {                         tabBarController.selectedIndex = controllerIndex;                     }                 }];

Solution 2: scroll (more complex)

A more complex solution, but gives you more control of the animation. In this example we get the views to slide on and off. With this one we need to manage the views ourselves. 复杂点的解决方案:

// Get the views. UIView * fromView = tabBarController.selectedViewController.view; UIView * toView = [[tabBarController.viewControllers objectAtIndex:controllerIndex] view]; // Get the size of the view area. CGRect viewSize = fromView.frame; BOOL scrollRight = controllerIndex > tabBarController.selectedIndex; // Add the to view to the tab bar view. [fromView.superview addSubview:toView]; // Position it off screen. toView.frame = CGRectMake((scrollRight ? 320 : -320), viewSize.origin.y, 320, viewSize.size.height); [UIView animateWithDuration:0.3                  animations: ^{                      // Animate the views on and off the screen. This will appear to slide.                      fromView.frame =CGRectMake((scrollRight ? -320 : 320), viewSize.origin.y, 320, viewSize.size.height);                      toView.frame =CGRectMake(0, viewSize.origin.y, 320, viewSize.size.height);                  }                  completion:^(BOOL finished) {                      if (finished) {                          // Remove the old view from the tabbar view.                          [fromView removeFromSuperview];                          tabBarController.selectedIndex = controllerIndex;                                      }                  }];

选择tab还是navigation?两个原则:

1/UITabBarController must be the root view of your window;2/tab bar 是无法修改的,但应该是可以修饰的@property(nonatomic,readonly) UITabBar *tabBar)

bading tab在tabitem上标示数字/比如存在多少未下载,纪录下来,审批完成的地方,纪录多少待上传/

在APP 商店程序中,我们就可以看到多少个更新的数字提示,不需要用户进入该tab后触发,在itune的下载tab中,badged数字表示活跃的当前下载数目,通过这个数字可以告诉客户下载完成了一个新得,而不需要打断客户,如果所有的下载全部完成,那么数字小时。

尽管我们一直看到的数字,但也有通过标有数字的icon来实现的。所有的uitabbaritem有一个属性badgevalue是个nsstring类型。设置后就可以出现一个泡泡,其中保函了数字。

import <UIKit/UIKit.h>@interface ThirdViewController : UIViewController

//methods

-(IBAction)badgeTabIcon:(id)sender;

-(IBAction)clearTabBadge:(id)sender;

@end

-(IBAction)badgeTabIcon:(id)sender{  

self.tabBarItem.badgeValue = @"Hi!";}

-(IBAction)clearTabBadge:(id)sender{  

self.tabBarItem.badgeValue = nil;}


=== =====关于accelerometer的问题,切换以及关键view

So I have an application that is based on a TabBarController and in one of my tabs Im using the UIAccelerometer. The problem Im seeing is that I can access and use the accelerometer just fine until I switch tabs and switch back. Say for instance my accelerometer is on tab1, and I click tab2 and back to tab1 the accelerometer is off.

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[self becomeFirstResponder];

[accelerometer setDelegate:self];
...}

注意顺序问题,以上代码的问题是调用了在willAppear/willXXXXX,实际应该在实现之后的didAppear。。。。。 。。。。。 。。。

it turns out that I needed to override viewDidAppear and viewDidDisappear methods in my viewController.

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];}

- (void)viewDidDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewDidDisappear:animated];}

After doing this, the motionBegan was called after switching between viewControllers. Hope this helps anyone out there... 如果切换到之前的tab,需要重设firstreponse


你可能感兴趣的:(application,UIView,tabs,transition,scroll,methods)