美文网首页
标题与View的联动(通过ScrollView实现)

标题与View的联动(通过ScrollView实现)

作者: 1073d142fdd4 | 来源:发表于2017-08-24 20:12 被阅读0次

    首先我们要创建几个View用来滚动下面是我示例的

    接下来就是ScrollView的实现了

    <UIScrollViewDelegate,UIPopoverPresentationControllerDelegate>{

    NSMutableArray *arr;

    UIButton *btn;

    }

    下面如上图所示


    @property (nonatomic, strong) UISegmentedControl *segmentedControl;

    @property (nonatomic, strong) UIScrollView *scrollView;

    @property (nonatomic, strong) OneViewController *oneVC;

    @property (nonatomic, strong) TwoViewController *twoVC;

    @property (nonatomic, strong) ThreeViewController *threeVC;

    @property (nonatomic, strong) FourViewController *fourVC;

    @property (nonatomic, strong) FiveViewController *fiveVC;

    @property (nonatomic, strong) SixViewController *sixVC;

    然后是一些具体的实现代码部分(viewDidLoad)

    //    创建一个view,放分段控制器

    UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 30)];

    //    vv.backgroundColor = [UIColor redColor];

    [self.view addSubview:vv];

    //    存储分段控制器标题

    arr = [NSMutableArray arrayWithObjects:@"移动通讯",@"传媒",@"软工",@"网工",@"云计算",@"建筑",nil];

    // 适应scrollView

    self.automaticallyAdjustsScrollViewInsets = NO;

    //    创建分段控制器

    self.segmentedControl = [[UISegmentedControl alloc]initWithItems:arr];

    self.segmentedControl.frame = CGRectMake(0, 0, 386, 30);

    //    加载到vv上

    [vv addSubview:self.segmentedControl];

    //    self.navigationItem.titleView = self.segmentedControl;

    [self.segmentedControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];

    //    默认第一个视图

    self.segmentedControl.selectedSegmentIndex = 0;

    // 创建scrollView

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 94, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64)];

    [self.view addSubview:self.scrollView];

    // 设置scrollView的内容

    self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 6, [UIScreen mainScreen].bounds.size.height - 64);

    self.scrollView.pagingEnabled = YES;

    self.scrollView.bounces = YES;

    // 创建控制器

    self.oneVC = [OneViewController new];

    self.twoVC = [TwoViewController new];

    self.threeVC = [ThreeViewController new];

    self.fourVC = [FourViewController new];

    self.fiveVC = [FiveViewController new];

    self.sixVC = [SixViewController new];

    // 添加为self的子控制器

    [self addChildViewController:self.oneVC];

    [self addChildViewController:self.twoVC];

    [self addChildViewController:self.threeVC];

    [self addChildViewController:self.fourVC];

    [self addChildViewController:self.fiveVC];

    [self addChildViewController:self.sixVC];

    //    每个视图

    self.oneVC.view.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

    self.twoVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

    self.threeVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*2, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

    self.fourVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*3, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

    self.fiveVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*4, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

    self.sixVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*5, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));

    //加载到滚动视图上

    [self.scrollView addSubview:self.oneVC.view];

    [self.scrollView addSubview:self.twoVC.view];

    [self.scrollView addSubview:self.fourVC.view];

    [self.scrollView addSubview:self.fiveVC.view];

    [self.scrollView addSubview:self.threeVC.view];

    [self.scrollView addSubview:self.sixVC.view];

    // 设置scrollView的代理

    self.scrollView.delegate = self;

    //    按钮

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    //按钮位置

    btn.frame = CGRectMake(385, 0, 30, 30);

    //    按钮背景颜色

    btn.backgroundColor = [UIColor whiteColor];

    [btn setTitle:@"+" forState:0];

    btn.layer.borderWidth = 1;

    btn.layer.borderColor = [UIColor blueColor].CGColor;

    //    按钮点击事件

    [btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];

    //    加载

    [vv addSubview:btn];

    代理方法

    //分段控制器方法

    - (void)segmentedControlAction:(UISegmentedControl *)sender

    {

    [self.scrollView setContentOffset:CGPointMake(sender.selectedSegmentIndex * self.scrollView.frame.size.width, 0) animated:YES];

    }

    //滚动视图代理方法

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

    {

    NSInteger n = scrollView.contentOffset.x / scrollView.frame.size.width;

    self.segmentedControl.selectedSegmentIndex = n;

    }

    相关文章

      网友评论

          本文标题:标题与View的联动(通过ScrollView实现)

          本文链接:https://www.haomeiwen.com/subject/jqtydxtx.html