iOS之tabBarItem选中变为“刷新”及再次点击刷新页面

2019-09-26  本文已影响0人  zwing

在自定义的UITabBarController的.m文件中实现以下代理方法即可:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if (viewController.tabBarController.selectedIndex == 3) {
        tabBarController.viewControllers[3].tabBarItem.title = @"刷新";
    } else {
        tabBarController.viewControllers[3].tabBarItem.title = @"资讯";
    }
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    // 判断哪个界面要需要再次点击刷新
    if ([tabBarController.selectedViewController isEqual:tabBarController.viewControllers[3]]) {
        // 判断再次选中的是否为当前的控制器
        if ([viewController isEqual:tabBarController.selectedViewController]) {
            // 执行操作
            [[NSNotificationCenter defaultCenter] postNotificationName:@"k_gg_tab_item_clicked_refresh" object:nil];
            
            // 如需要在再次点击的时候,改变icon并做动画,动画结束复原,则需加上一下代码👇:
            viewController.tabBarItem.selectedImage = [UIImage imageNamed:@"tab_刷新选中"];

            UIControl *tabBarButton = [viewController.tabBarItem valueForKey:@"view"];
            UIImageView *tabBarSwappableImageView = [tabBarButton valueForKey:@"info"];
            
            CABasicAnimation * rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; //让其在z轴旋转
            rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];//旋转角度
            rotationAnimation.duration = 0.5; //旋转周期
            rotationAnimation.cumulative = YES;//旋转累加角度
            rotationAnimation.repeatCount = 1;//旋转次数
            [tabBarSwappableImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                viewController.tabBarItem.selectedImage = [UIImage imageNamed:@"tab_资讯选中"];
            });
            
            return NO;
        }
    }
    return YES;
}
上一篇下一篇

猜你喜欢

热点阅读