iOS TabBarController的幾種動(dòng)畫效果的實(shí)現(xiàn)

原理:利用UITabBarController實(shí)現(xiàn),在tabbar的 didSelectItem 代理里添加動(dòng)畫效果。

@interface MainTabbarVC()@property (nonatomic,assign) NSInteger indexFlag;  //記錄上一次點(diǎn)擊tabbar,使用時(shí),記得先在init或viewDidLoad里 初始化 = 0

@end

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

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

? ? if (index != self.indexFlag) {

? ? ? ? //執(zhí)行動(dòng)畫? ? ??

? NSMutableArray *arry = [NSMutableArray array];

? ? ? ? for (UIView *btn in self.tabBar.subviews) {

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

? ? ? ? ? ? ? ? [arry addObject:btn];

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? //添加動(dòng)畫

?//---將下面的代碼塊直接拷貝到此即可---? ? ? ??

self.indexFlag = index;

? ? }

}


1、先放大,再縮小


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

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//速度控制函數(shù),控制動(dòng)畫運(yùn)行的節(jié)奏

animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

animation.duration = 0.2;? ? ? //執(zhí)行時(shí)間

animation.repeatCount = 1;? ? ? //執(zhí)行次數(shù)

animation.autoreverses = YES;? ? //完成動(dòng)畫后會(huì)回到執(zhí)行動(dòng)畫之前的狀態(tài)

animation.fromValue = [NSNumber numberWithFloat:0.7];? //初始伸縮倍數(shù)

animation.toValue = [NSNumber numberWithFloat:1.3];? ? //結(jié)束伸縮倍數(shù)

[[arry[index] layer] addAnimation:animation forKey:nil];



2、Z軸旋轉(zhuǎn)

//z軸旋轉(zhuǎn)180度

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//速度控制函數(shù),控制動(dòng)畫運(yùn)行的節(jié)奏

animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

animation.duration = 0.2;? ? ? //執(zhí)行時(shí)間

animation.repeatCount = 1;? ? ? //執(zhí)行次數(shù)

animation.removedOnCompletion = YES;

animation.fromValue = [NSNumber numberWithFloat:0];? //初始伸縮倍數(shù)

animation.toValue = [NSNumber numberWithFloat:M_PI];? ? //結(jié)束伸縮倍數(shù)

[[arry[index] layer] addAnimation:animation forKey:nil];


3、Y軸位移


//向上移動(dòng)

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];//速度控制函數(shù),控制動(dòng)畫運(yùn)行的節(jié)奏

animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

animation.duration = 0.2;? ? ? //執(zhí)行時(shí)間

animation.repeatCount = 1;? ? ? //執(zhí)行次數(shù)

animation.removedOnCompletion = YES;

animation.fromValue = [NSNumber numberWithFloat:0];? //初始伸縮倍數(shù)

animation.toValue = [NSNumber numberWithFloat:-10];? ? //結(jié)束伸縮倍數(shù)

[[arry[index] layer] addAnimation:animation forKey:nil];



4、放大并保持


//放大效果

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//速度控制函數(shù),控制動(dòng)畫運(yùn)行的節(jié)奏

animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

animation.duration = 0.2;? ? ? //執(zhí)行時(shí)間

animation.repeatCount = 1;? ? ? //執(zhí)行次數(shù)

animation.removedOnCompletion = NO;

animation.fillMode = kCAFillModeForwards;? ? ? ? ? //保證動(dòng)畫效果延續(xù)

animation.fromValue = [NSNumber numberWithFloat:1.0];? //初始伸縮倍數(shù)

animation.toValue = [NSNumber numberWithFloat:1.15];? ? //結(jié)束伸縮倍數(shù)

[[arry[index] layer] addAnimation:animation forKey:nil];

//移除其他tabbar的動(dòng)畫

for (int i = 0; i<arry.count; i++) {

? ? if (i != index) {

? ? ? ? [[arry[i] layer] removeAllAnimations];

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容