原理:利用UITabBarController實現(xiàn)蒂破,在tabbar的 didSelectItem 代理里添加動畫效果。
@interface MainTabbarVC()@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) {
? ? ? ? //執(zhí)行動畫? ? ??
? NSMutableArray *arry = [NSMutableArray array];
? ? ? ? for (UIView *btn in self.tabBar.subviews) {
? ? ? ? ? ? if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
? ? ? ? ? ? ? ? [arry addObject:btn];
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //添加動畫
?//---將下面的代碼塊直接拷貝到此即可---? ? ? ??
self.indexFlag = index;
? ? }
}
1司光、先放大误褪,再縮小
//放大效果,并回到原位
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//速度控制函數(shù)肩钠,控制動畫運行的節(jié)奏
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.duration = 0.2;? ? ? //執(zhí)行時間
animation.repeatCount = 1;? ? ? //執(zhí)行次數(shù)
animation.autoreverses = YES;? ? //完成動畫后會回到執(zhí)行動畫之前的狀態(tài)
animation.fromValue = [NSNumber numberWithFloat:0.7];? //初始伸縮倍數(shù)
animation.toValue = [NSNumber numberWithFloat:1.3];? ? //結束伸縮倍數(shù)
[[arry[index] layer] addAnimation:animation forKey:nil];
2泣港、Z軸旋轉
//z軸旋轉180度
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//速度控制函數(shù),控制動畫運行的節(jié)奏
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.duration = 0.2;? ? ? //執(zhí)行時間
animation.repeatCount = 1;? ? ? //執(zhí)行次數(shù)
animation.removedOnCompletion = YES;
animation.fromValue = [NSNumber numberWithFloat:0];? //初始伸縮倍數(shù)
animation.toValue = [NSNumber numberWithFloat:M_PI];? ? //結束伸縮倍數(shù)
[[arry[index] layer] addAnimation:animation forKey:nil];
3价匠、Y軸位移
//向上移動
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];//速度控制函數(shù)当纱,控制動畫運行的節(jié)奏
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.duration = 0.2;? ? ? //執(zhí)行時間
animation.repeatCount = 1;? ? ? //執(zhí)行次數(shù)
animation.removedOnCompletion = YES;
animation.fromValue = [NSNumber numberWithFloat:0];? //初始伸縮倍數(shù)
animation.toValue = [NSNumber numberWithFloat:-10];? ? //結束伸縮倍數(shù)
[[arry[index] layer] addAnimation:animation forKey:nil];
4、放大并保持
//放大效果
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//速度控制函數(shù)踩窖,控制動畫運行的節(jié)奏
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.duration = 0.2;? ? ? //執(zhí)行時間
animation.repeatCount = 1;? ? ? //執(zhí)行次數(shù)
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;? ? ? ? ? //保證動畫效果延續(xù)
animation.fromValue = [NSNumber numberWithFloat:1.0];? //初始伸縮倍數(shù)
animation.toValue = [NSNumber numberWithFloat:1.15];? ? //結束伸縮倍數(shù)
[[arry[index] layer] addAnimation:animation forKey:nil];
//移除其他tabbar的動畫
for (int i = 0; i<arry.count; i++) {
? ? if (i != index) {
? ? ? ? [[arry[i] layer] removeAllAnimations];
? ? }
}