设置viewcontroller的 self.title 会使tabbaritem的对象改变

美工要求再点击tabbar的时候做一个动画效果,我开始没时间 同事帮忙写好了,也就几行代码。
通过找到点击的tabbarbutton 遍历到上面的image的view,然后在上面的layer层做一个动画。

#pragma mark UITabBarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
    NSInteger index = [tabBar.items indexOfObject:item];
    if (index == self.selectedIndex) {
        return;
    }
    
    //lastAnimationObject 用来记录上次点击的对象
    [self.lastAnimationObject.layer removeAnimationForKey:@"bounce"];
    [self animationWithIndex:index];
}

- (void)animationWithIndex:(NSInteger)index {
    
    NSMutableArray *animationObjects = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            for (UIView *tabBarSwappableImageView in tabBarButton.subviews) {
                if ([tabBarSwappableImageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
                    [animationObjects addObject:tabBarSwappableImageView];
                }
            }
        }
    }
    
    UIView *viewObject = [animationObjects objectAtIndex:index];
    [self bounceAnimationWithAnimationObjects:viewObject index:index];
}

- (void)bounceAnimationWithAnimationObjects:(UIView *)viewObject index:(NSInteger)index {
    
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    keyFrameAnimation.values = @[@1.0 ,@1.4, @0.9, @1.15, @0.95, @1.02, @1.0];
    keyFrameAnimation.duration = 0.5;
    keyFrameAnimation.calculationMode = kCAAnimationCubic;
    [viewObject.layer addAnimation:keyFrameAnimation forKey:@"bounce"];
    self.lastAnimationObject = viewObject;
}

开始运行的时候没问题,后来测试反馈点击第三个的时候第四个做动画,第四个的时候第三个做动画。通过打印便利对象发现是因为点击点三个的时候第三个button的对象发生了改变重新创建了一次,所以在数组中三四的对象的位置是相反的。

正常情况下的animationObjects的数组中的数据

点击index3 后,再点击index4 发生了改变,实际上是整个tabBarButton的对象都变了

index3的tabBarButton发生了改变 所以数组中被放到了最后一位

所以这时候我通过index3取到的对象实际上是index4的对象。会造成点击index3 index4动画,点击index4 index3做动画。

代码反复看了好多遍没发现问题在哪。后来发现我这第三个的vc里面执行了一次 self.title= @"xxxx" ;

image.png

这个会同时改变 navigationController的title 和 tabBarItem的title,而且tabBarButton的对象也发生了改变。

但是,如果你通过self.title这是的title 和初始化时tabBarItem的title一致的话,tabBarButton却不会发生改变。

用 self.navigationController.title = @"我的收藏"; 或 self.navigationItem.title = @"我的收藏";
来设置导航的title 也不会影响。

第一次发现这个问题 写下了 以后注意!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 3,509评论 2 4
  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 1,596评论 2 4
  • 这个世界有很多学问我们想要了解,像是艺术方面,当我们看到莫内美丽的水彩素描画时,我们心里无比向往;听到一首美丽的民...
    思想筆記阅读 478评论 6 5
  • 近日发现VS2015在启动时的一个有趣的地方:VS在启动时会有一个启动窗体,可以用鼠标拖动这个窗体,在VS启动完成...
    DotNet患者阅读 321评论 0 0
  • 她是小国公主,因生得惊艳动人,被前往他国做质子。 他是雄国将军,因皇帝年幼权势滔天。 她被送来那一日,正逢他战争胜...
    jiuke的伤阅读 295评论 0 0