TabBarController的几种动画效果的实现

利用UITabBarController实现,在tabbar的 didSelectItem 代理里添加动画效果。

@interfaceMainTabbarVC()<UITabBarControllerDelegate>

@property(nonatomic,assign)NSInteger indexFlag;//记录上一次点击tabbar,使用时,记得先在init或viewDidLoad里 初始化 = 0

@end

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

NSInteger index=[self.tabBar.items indexOfObject:item];

if(index!=self.indexFlag){//执行动画

NSMutableArray*arry=[NSMutableArray array];

for(UIView*btninself.tabBar.subviews){

if([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]){

[arry addObject:btn];

}

}

//添加动画//---将下面的代码块直接拷贝到此即可---

self.indexFlag=index;

}

}



1、先放大,再缩小

//放大效果,并回到原位

CABasicAnimation*animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];

//速度控制函数,控制动画运行的节奏

animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

animation.duration=0.2;//执行时间

animation.repeatCount=1;//执行次数

animation.autoreverses=YES;//完成动画后会回到执行动画之前的状态

animation.fromValue=[NSNumber numberWithFloat:0.7];//初始伸缩倍数

animation.toValue=[NSNumber numberWithFloat:1.3];//结束伸缩倍数[[arry[index]layer]addAnimation:animation forKey:nil];

2、Z轴旋转

//z轴旋转180度

CABasicAnimation*animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

//速度控制函数,控制动画运行的节奏

animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

animation.duration=0.2;//执行时间

animation.repeatCount=1;//执行次数animation.removedOnCompletion=YES;

animation.fromValue=[NSNumber numberWithFloat:0];//初始伸缩倍数

animation.toValue=[NSNumber numberWithFloat:M_PI];//结束伸缩倍数[[arry[index]layer]addAnimation:animation forKey:nil];

3、放大并保持

//放大效果

CABasicAnimation*animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];

//速度控制函数,控制动画运行的节奏

animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

animation.duration=0.2;//执行时间

animation.repeatCount=1;//执行次数animation.removedOnCompletion=NO;

animation.fillMode=kCAFillModeForwards;//保证动画效果延续

animation.fromValue=[NSNumber numberWithFloat:1.0];//初始伸缩倍数

animation.toValue=[NSNumber numberWithFloat:1.15];//结束伸缩倍数[[arry[index]layer]addAnimation:animation forKey:nil];//移除其他tabbar的动画for(inti=0;i<arry.count;i++){

if(i!=index){

[[arry[i]layer]removeAllAnimations];

}

}

4、 y轴位移

//向上移动

CABasicAnimation*animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];

//速度控制函数,控制动画运行的节奏

animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

animation.duration=0.2;//执行时间

animation.repeatCount=1;//执行次数

animation.removedOnCompletion=YES;

animation.fromValue=[NSNumber numberWithFloat:0];//初始伸缩倍数

animation.toValue=[NSNumber numberWithFloat:-10];//结束伸缩倍数[[arry[index]layer]addAnimation:animation forKey:nil];

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

推荐阅读更多精彩内容