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

[置顶] iOS中tabBar按钮再次点击实现界面刷新

2017-09-07 19:47 579 查看
/** 记录上一次被点击按钮的tag */
@property (nonatomic, assign) NSInteger previousClickedTag;


- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;

self.view.backgroundColor = HWColor(245, 245, 245);
[self setNavigationContent];

//    self.tabBarItem addObserver:<#(nonnull NSObject *)#> forKeyPath:<#(nonnull NSString *)#> options:<#(NSKeyValueObservingOptions)#> context:<#(nullable void *)#>
self.tabBarController.delegate = self;

self.previousClickedTag = 100;//默认没有点击任何tabbar

}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

if (tabBarController.selectedIndex == 0  && [self.tabBarItem.title isEqualToString:homeTabbarSelectedTitle]) {

//进行数据刷新

if ( self.previousClickedTag ==  tabBarController.selectedIndex ) {//进行了第二次点击

[self.tableView.mj_header beginRefreshing];
}

}

self.previousClickedTag = tabBarController.selectedIndex;//记录上一次按钮的点击

}


通过代理方法didSelectItem 修改UITabBarItem的title ,达到选中之后和未选中的title不一样的效果

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{

if ([item.title isEqualToString:GYQhomeTabbarTitle]) {
item.title = homeTabbarSelectedTitle;
}else{

for (UITabBarItem *childView in tabBar.items) {
if ([childView.title isEqualToString:GYQhomeTabbarSelectedTitle] &&  childView != item) {
childView.title = homeTabbarTitle;

}

}

}

}


旋转tabbar 的图片

self.previousClickedTag = 100;//默认没有点击任何tabbar

HWTabBar *tabBar = (HWTabBar*)self.tabBarController.tabBar;
[tabBar setBlock:^(UIView * imageView){

if (self.isreloadData) {

self.imageView = imageView;
CABasicAnimation * rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; //让其在z轴旋转
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];//旋转角度
rotationAnimation.duration = 2; //旋转周期
rotationAnimation.cumulative = YES;//旋转累加角度
rotationAnimation.repeatCount = 100000;//旋转次数
[imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

}

}];

}

-(void)stopRotate {
[self.imageView.layer removeAllAnimations];
}

- (void)setIsreloadData:(BOOL)isreloadData{
_isreloadData = isreloadData;
if (!isreloadData) {
[self stopRotate];
}
}


//进行数据刷新

if ( self.previousClickedTag ==  tabBarController.selectedIndex ) {//进行了第二次点击

[self.tableView.mj_header beginRefreshing];
self.isreloadData = YES;
}


监听事件的点击

// 遍历tabBar上的子控件,给"UITabBarButton"类型的按钮绑定动画效果事件
//(注意:遍历添加动画事件的时机是在layoutSubviews布局子控件方法中)
- (void)layoutSubviews{
[super layoutSubviews];

for (UIControl *tabBarButton in self.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
}

// 动画效果(遍历UITabBarButton按钮的子控件,
//如果需要对图片添加动画,寻找"UITabBarSwappableImageView"类型的图片子控件;
////如果需要对按钮下面的文字添加动画,寻找"UITabBarButtonLabel"类型的文字子控件即可).
- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
for (UIView *imageView in tabBarButton.subviews) {
if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
if (self.block) {
self.block(imageView);
}
}}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐