溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS多控制器如何實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫

發(fā)布時(shí)間:2021-08-06 14:02:27 來源:億速云 閱讀:121 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章主要為大家展示了“iOS多控制器如何實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“iOS多控制器如何實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫”這篇文章吧。

主控制器 ,管理控制器 .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 , 用于滑動(dòng)

@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;
}

初始化頂部選擇欄

//三個(gè)子控制器
- (NSArray *)VcNames
{
  if (!_VcNames) {
    _VcNames = @[@"控制器一",@"控制器二",@"控制器三"];
  }
  return _VcNames;
}

//點(diǎn)擊選擇欄
- (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};
}

//初始化滑動(dòng)容器
- (void)initScrollViewContainer
{
  [self.view addSubview:self.containerScrollerView];
  self.containerScrollerView.frame = CGRectMake(0,44,kScreenWidth, kScreenHeight );
}

//添加子控制器
- (void)addChildControllers
{
  //為了方便直觀 , 在此處設(shè)置背景色 (實(shí)際開發(fā)中,不能在這里設(shè)置 , 原因是這里只要調(diào)用到了控制器的view屬性 , 該控制器將會執(zhí)行viewDidLoad方法 , 相當(dāng)于直接一開始就將三個(gè)控制器的所有UI和網(wǎng)絡(luò)請求全加載完了 , 負(fù)荷會相當(dāng)重)
  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];

  //默認(rèn)展示第一個(gè)子控制器
  [self scrollViewDidEndDecelerating:self.containerScrollerView];
}

按鈕點(diǎn)擊事件實(shí)現(xiàn) , 代理方法實(shí)現(xiàn)

//選擇欄按鈕點(diǎn)擊事件
- (void)buttonClick:(UIButton *)button
{
  [self.containerScrollerView setContentOffset:CGPointMake(button.tag *kScreenWidth, 0) animated:YES];
}

//滑動(dòng)減速時(shí)調(diào)用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  //獲取contentOffset
  CGPoint currentOffset = scrollView.contentOffset;

  NSInteger page = currentOffset.x / kScreenWidth;

  //取出對應(yīng)控制器
  UIViewController *viewController = self.childViewControllers12下一頁閱讀全文

以上是“iOS多控制器如何實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI