segemtController 切换视图(可以左右滑动)

该博客介绍了一个实现通过UISegmentedControl切换UIScrollView内多个视图的方法,包括设置子控制器、添加到scrollview以及内容切换动画。通过监听UISegmentedControl的事件,根据选中的索引更新UIScrollView的contentOffset,从而实现左右滑动效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




//

//  ViewController.m

//  scrollviewcollectionview

//

//  Created by 洪福清 on 16/11/29.

//  Copyright © 2016 BJTYL. All rights reserved.

//


#import "ViewController.h"

#import "SecondViewController.h"

#import "ThridViewController.h"

#import "UIColor+SPAddition.h"


#define SP_NavitionBarBackColor     @"#1894d1"



@interface ViewController ()<UIScrollViewDelegate>


@property (strong, nonatomic) UISegmentedControl *segmentControl;



@property (strong, nonatomic) UIScrollView *scrollView;



@end


@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor whiteColor];

    

    // 初始化子控制器

    [self setupAllChildVcs];

    

    [self addController];


    // 添加第0个子控制器的view

    [self addChildVcViewIntoScrollView:0];

}



- (void)setupAllChildVcs

{

    [self addChildViewController:[[SecondViewController alloc] init]];

    [self addChildViewController:[[ThridViewController alloc] init]];

 

}


- (void)addController

{

    [self.view addSubview:self.segmentControl];

    [self.view addSubview:self.scrollView];

    

    

}




-(UISegmentedControl *)segmentControl

{

    if (_segmentControl == nil) {

        NSArray *array = [NSArray arrayWithObjects:@"候车信息",@"到达信息",nil];

        _segmentControl = [[UISegmentedControl alloc] initWithItems:array];

        _segmentControl.selectedSegmentIndex = 0;

        _segmentControl.frame = CGRectMake(50, 70, self.view.frame.size.width-100, 40);

        _segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;

        _segmentControl.tintColor = [UIColor colorWithHexString:SP_NavitionBarBackColor];

        [_segmentControl addTarget:self action:@selector(segemntControlClick:) forControlEvents:UIControlEventValueChanged];

    }

    return _segmentControl;

}


-(UIScrollView *)scrollView

{

    if (_scrollView == nil) {

        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 110, self.view.frame.size.width, self.view.frame.size.height-110)];

        _scrollView.delegate = self;

        _scrollView.pagingEnabled = YES;

        _scrollView.contentSize = CGSizeMake(self.view.frame.size.width*2, 0);

    }

    return _scrollView;

}



- (void)segemntControlClick:(UISegmentedControl *)segment

{

    switch (segment.selectedSegmentIndex) {

        case 0:

        {

            [UIView animateWithDuration:0.25 animations:^{

                self.scrollView.contentOffset = CGPointMake(0, 0);

            } completion:^(BOOL finished) {

                // 添加子控制器的view

                [self addChildVcViewIntoScrollView:0];

            }];

        }

            break;

        case 1:

        {

            [UIView animateWithDuration:0.25 animations:^{

                self.scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);

            } completion:^(BOOL finished) {

                // 添加子控制器的view

                [self addChildVcViewIntoScrollView:1];

            }];

        }

            

        default:

            break;

    }

}



- (void)addChildVcViewIntoScrollView:(NSUInteger)index

{

    UIViewController *childVc = self.childViewControllers[index];

    

    // 如果view已经被加载过,就直接返回

    if (childVc.isViewLoaded) return;

    

    // 取出index位置对应的子控制器view

    UIView *childVcView = childVc.view;

    

    // 设置子控制器viewframe

    CGFloat scrollViewW = self.scrollView.frame.size.width;

    childVcView.frame = CGRectMake(index * scrollViewW, 0, scrollViewW, self.scrollView.frame.size.height);

    // 添加子控制器的viewscrollView

    [self.scrollView addSubview:childVcView];

}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    // 1.取出水平方向上滚动的距离

    CGFloat offsetX = scrollView.contentOffset.x;

    

    // 2.求出页码

    double pageDouble = offsetX / scrollView.frame.size.width;

    int pageInt = (int)(pageDouble + 0.5);

    self.segmentControl.selectedSegmentIndex = pageInt;

}




@end




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值