https://blog.csdn.net/qq_43441647/article/details/143366477ba
//這是自定義tabbar里面的selectedIndex的方法,按其他那個加代理,自己定義動畫的那個方法沒起作用。這個方法可以
- (void)setSelectedIndex:(NSUInteger)selectedIndex{
? ? if(selectedIndex >=self.viewControllers.count){
? ? ? ? @throw [NSException exceptionWithName:@"selectedTabbarError"
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? reason:@"No controller can be used,Because of index beyond the viewControllers,Please check the configuration of tabbar."
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? userInfo:nil];
? ? }
? ? //取掉iOS 18tabbar縮放動畫,如果不去掉,頁面會有縮放效果,先下拉然后恢復
? ? if(@available(iOS18.0, *)) {
? ? ? ? [UIView performWithoutAnimation:^{
? ? ? ? ? ? [supersetSelectedIndex:selectedIndex];
? ? ? ? }];
? ? }else{
? ? ? ? [supersetSelectedIndex:selectedIndex];
? ? }
? ? [self.cyTabbar setValue:[NSNumber numberWithInteger:selectedIndex] forKeyPath:@"selectButtoIndex"];
}
//這中在代理里面自定義切換動畫的方式,寫了不起作用
@interface TabBarController () <UIViewControllerAnimatedTransitioning, UITabBarControllerDelegate>
@end
@implementation TabBarController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? //設置代理
? ? self.delegate = self;
}
#pragma mark - UITabBarControllerDelegate
- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
? ? ? ? ? ? ? ? ? ? animationControllerForTransitionFromViewController:(UIViewController *)fromVC
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? toViewController:(UIViewController *)toVC {
? ? return self;
}
#pragma mark - UIViewControllerAnimatedTransitioning
- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
? ? UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
? ? [transitionContext.containerView addSubview:toView];
? ? [transitionContext completeTransition:YES];
}
//返回0s禁用動畫
- (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext {
? ? return 0;
}
@end