温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS多控制器如何实现带滑动动画

发布时间:2021-08-06 14:02:27 来源:亿速云 阅读:173 作者:小新 栏目:移动开发

这篇文章主要为大家展示了“iOS多控制器如何实现带滑动动画”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“iOS多控制器如何实现带滑动动画”这篇文章吧。

主控制器 ,管理控制器 .h文件

//宏 #define kScreenWidth [UIScreen mainScreen].bounds.size.width #define kScreenHeight [UIScreen mainScreen].bounds.size.height #import "MYMainViewController.h" #import "MYFirstViewController.h" #import "MYSecondViewController.h" #import "MYThirdViewController.h" @interface MYMainViewController ()<UIScrollViewDelegate> //控制器名 @property (nonatomic, strong) NSArray *VcNames; //选择栏 @property(nonatomic, strong) UIView *clickBar; //底部容器scrollView @property (strong, nonatomic) UIScrollView *containerScrollerView; @end

. m 文件

底部scrollView , 用于滑动

@implementation MYMainViewController - (UIScrollView *)containerScrollerView {   if (!_containerScrollerView) {     _containerScrollerView = [[UIScrollView alloc]init];     _containerScrollerView.pagingEnabled = YES;     _containerScrollerView.showsVerticalScrollIndicator = NO;     _containerScrollerView.showsHorizontalScrollIndicator = NO;     _containerScrollerView.contentSize = CGSizeMake(kScreenWidth *self.VcNames.count,kScreenHeight);     _containerScrollerView.backgroundColor = [UIColor whiteColor];     _containerScrollerView.delegate = self;   }   return _containerScrollerView; }

初始化顶部选择栏

//三个子控制器 - (NSArray *)VcNames {   if (!_VcNames) {     _VcNames = @[@"控制器一",@"控制器二",@"控制器三"];   }   return _VcNames; } //点击选择栏 - (UIView *)clickBar {   if (!_clickBar) {     _clickBar = [[UIView alloc]init];     _clickBar.backgroundColor = [UIColor lightGrayColor];     CGFloat width = kScreenWidth / 3;     CGFloat height = 44;     //初始化按钮     for (NSInteger index = 0; index < 3; index++) {       UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];       [button setTitle:self.VcNames[index] forState:UIControlStateNormal];       [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];       button.frame = (CGRect){width *index,0,width,height};       [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];       //绑定tag值       button.tag = index;       [_clickBar addSubview:button];     }   }   return _clickBar; }

viewDidLoad

- (void)viewDidLoad {   [super viewDidLoad];   self.edgesForExtendedLayout = 0;   //初始化选择栏   [self initClickBar];   //初始化底部scrollView容器   [self initScrollViewContainer];   //初始化子控制器   [self addChildControllers]; }

添加子控制器 , 初始化UI

//按钮选择栏 - (void)initClickBar {   [self.view addSubview:self.clickBar];   self.clickBar.frame = (CGRect){0,0,[UIScreen mainScreen].bounds.size.width,44}; } //初始化滑动容器 - (void)initScrollViewContainer {   [self.view addSubview:self.containerScrollerView];   self.containerScrollerView.frame = CGRectMake(0,44,kScreenWidth, kScreenHeight ); } //添加子控制器 - (void)addChildControllers {   //为了方便直观 , 在此处设置背景色 (实际开发中,不能在这里设置 , 原因是这里只要调用到了控制器的view属性 , 该控制器将会执行viewDidLoad方法 , 相当于直接一开始就将三个控制器的所有UI和网络请求全加载完了 , 负荷会相当重)   MYFirstViewController *firstVc = [[MYFirstViewController alloc]init];   firstVc.view.backgroundColor = [UIColor redColor];   [self addChildViewController:firstVc];   MYSecondViewController *secondVc = [[MYSecondViewController alloc]init];   secondVc.view.backgroundColor = [UIColor blueColor];   [self addChildViewController:secondVc];   MYThirdViewController *thirdVc = [[MYThirdViewController alloc]init];   thirdVc.view.backgroundColor = [UIColor yellowColor];   [self addChildViewController:thirdVc];   //默认展示第一个子控制器   [self scrollViewDidEndDecelerating:self.containerScrollerView]; }

按钮点击事件实现 , 代理方法实现

//选择栏按钮点击事件 - (void)buttonClick:(UIButton *)button {   [self.containerScrollerView setContentOffset:CGPointMake(button.tag *kScreenWidth, 0) animated:YES]; } //滑动减速时调用 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {   //获取contentOffset   CGPoint currentOffset = scrollView.contentOffset;   NSInteger page = currentOffset.x / kScreenWidth;   //取出对应控制器   UIViewController *viewController = self.childViewControllers12下一页阅读全文

以上是“iOS多控制器如何实现带滑动动画”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios
AI