若希望 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。