1. 程式人生 > >給tabBarItem加點擊效果動畫

給tabBarItem加點擊效果動畫

dsc value ota remove The mar 喜歡 prope dex

獲取到tabBarItem,添加喜歡的動畫

.h文件

@interface JGTabBarController ()

//記錄上一次點擊tabbar
@property (nonatomic, assign) NSInteger indexFlag;

@end

.m文件

①先放大,再縮小;②Z軸旋轉 ;③向上移動;④放大並保持
@implementation JGTabBarController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
self.indexFlag = 0; } #pragma mark - UITabBarDelegate - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { /** 給 tabBarButton 加動畫 */ NSInteger index = [self.tabBar.items indexOfObject:item]; if (index != self.indexFlag) { //執行動畫 NSMutableArray *arry = [NSMutableArray array];
for (UIView *btn in self.tabBar.subviews) { if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) { [arry addObject:btn]; } } //添加動畫 [self addUpTranslationAnimtaionWithArr:arry index:index]; self.indexFlag
= index; } } #pragma mark - More Animation /// 先放大,再縮小 - (void)addScaleAnimtaionWithArr:(NSMutableArray *)arry index:(NSInteger)index { //放大效果,並回到原位 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]; } /// Z軸旋轉 - (void)addRotationAnimtaionWithArr:(NSMutableArray *)arry index:(NSInteger)index { //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]; } /// 向上移動 - (void)addUpTranslationAnimtaionWithArr:(NSMutableArray *)arry index:(NSInteger)index { //向上移動 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]; } /// 放大並保持 - (void)addscaleAndKeepAnimtaionWithArr:(NSMutableArray *)arry index:(NSInteger)index { //放大效果 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 (int i = 0; i<arry.count; i++) { if (i != index) { [[arry[i] layer] removeAllAnimations]; } } }

@end

UITabbarItem自定義 先放大再復原

//添加動畫
- (void)cusTabbarItemAddAnimationWithBtn:(UIButton *)btn {
    
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.2;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [btn.layer addAnimation:pulse forKey:nil];
}

給tabBarItem加點擊效果動畫