您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

為自訂 tabBar 按鈕加入點擊縮放動畫

編輯:iOS 開發綜合

若希望 tabBar 在切換時有更明顯的回饋,可以為按鈕加入縮放動畫。以下範例透過 UITabBarController 的 didSelectItem 方法,在點擊後對目前選取的 tabBarButton 播放動畫。

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger index = [self.tabBar.items indexOfObject:item];
    [self animationWithIndex:index];
}

- (void)animationWithIndex:(NSInteger)index {
    NSMutableArray *tabbarbuttonArray = [NSMutableArray array];

    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }

    CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.08;
    pulse.repeatCount = 1;
    pulse.autoreverses = YES;
    pulse.fromValue = [NSNumber numberWithFloat:0.7];
    pulse.toValue = [NSNumber numberWithFloat:1.3];

    [[tabbarbuttonArray[index] layer] addAnimation:pulse forKey:nil];
}

這個做法不需要額外引入第三方套件,就能讓底部導覽列在點擊時產生輕量化縮放效果。若需要更細緻的動畫,也可以再調整 duration、fromValue、toValue 與 timingFunction。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved