您的位置:首页 > 移动开发 > IOS开发

IOS_实现TabBar切换时底部切换效果

2013-07-03 14:45 681 查看
1.首先实现代理方法

#pragma mark -- UITabBarControllerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
UIImageView* slider = (UIImageView*)[self.tabBar viewWithTag:TAB_SLIDER_TAG];

NSUInteger selectedIndex = [self.viewControllers indexOfObjectIdenticalTo:viewController];
if (slider)
{
[self.tabBar bringSubviewToFront:slider];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
CGRect frame = slider.frame;
frame.origin.x = [self horizontalLocationFor:selectedIndex];
slider.frame = frame;
[UIView commitAnimations];
}
else
{
[self addTabBarSliderAtIndex:selectedIndex];
}
}

2.步骤1中又两个方法,需要实现下一个是水平移动、一个是更改图片
- (void)addTabBarSliderAtIndex:(NSUInteger)itemIndex
{
UIImage* sliderImage = [UIImage imageNamed:@"tab_slider"];
UIImageView* slider = [[UIImageView alloc] initWithImage:sliderImage] ;
slider.tag = TAB_SLIDER_TAG;

slider.frame = CGRectMake([self horizontalLocationFor:itemIndex]
, self.tabBar.height - sliderImage.size.height
, sliderImage.size.width
, sliderImage.size.height);

[self.tabBar addSubview:slider];
}

- (CGFloat) horizontalLocationFor:(NSUInteger)tabIndex
{

CGFloat tabItemWidth = self.tabBar.frame.size.width / self.viewControllers.count;
CGFloat halfTabItemWidth = (tabItemWidth / 2.0) - (_sliderWidth / 2.0);
return (tabIndex * tabItemWidth) + halfTabItemWidth;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Tabbar iOS