scrollview和pageControl

我们通过scrollview和pageControl可以实现一个欢迎页面。下面我就就来一起可以下设置两个空间,以及控件之间如何实现通信。
首先我们需要定义两个空间,以及遵守delegate协议

@interface ViewController () <UIScrollViewDelegate>
@property(nonatomic)UIScrollView *scrollview;
@property(nonatomic)UIPageControl *pageControl;
@end

scrollview懒加载

-(UIScrollView *)scrollview {
    if (!_scrollview) {
        _scrollview = [[UIScrollView alloc]init];
        _scrollview.backgroundColor = [UIColor redColor];
        _scrollview.frame = self.view.frame;
        _scrollview.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height);
        for (NSInteger i = 0; i < 4; i ++) {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height)];
            NSString *imageName = [NSString stringWithFormat:@"welcome%ld.PNG", i + 1];
//            NSString *path = [[NSBundle mainBundle]pathForResource:imageName ofType:nil];
//            imageView.image = [UIImage imageWithContentsOfFile:path];
            imageView.image = [UIImage imageNamed:imageName];
            [_scrollview addSubview:imageView];
            if (i == 3) {  //最后一个imageView
                //imageView 默认是不可以与用户交互的
                //打开imageView 的用户交互
                imageView.userInteractionEnabled = YES;
                UIButton *button = [self createButton];
                button.frame = CGRectMake((imageView.frame.size.width - 300) * 0.5, 450, 300, 80);
                [imageView addSubview:button];
            }
        }
        //设置 scrollview 整页滑动
        _scrollview.pagingEnabled = YES;
        //隐藏水平方向 灰条
        _scrollview.showsHorizontalScrollIndicator = NO;
        //去掉弹性
        _scrollview.bounces = NO;
        //设置代理
        _scrollview.delegate = self;
        [self.view addSubview:_scrollview];
    }
    return _scrollview;
}

pageControl懒加载

-(UIPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]init];
        _pageControl.frame = CGRectMake(0, self.view.frame.size.height - 20 - 40, self.view.frame.size.width, 40);
        //设置圆点个数
        _pageControl.numberOfPages = 4;
        //当前选中的是 哪个点  默认是0
        _pageControl.currentPage = 0;
        //设置未选中圆点 颜色
        _pageControl.pageIndicatorTintColor = [UIColor redColor];
        //设置选中点的颜色
        _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
        //关闭用户交互
        _pageControl.userInteractionEnabled = NO;
        [self.view addSubview:_pageControl];
    }
    return _pageControl;
}

创建按钮以及调用懒加载

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self scrollview];
    [self pageControl];
}

-(UIButton*)createButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor grayColor];
    [button setTitle:@"进入" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:24];
    [button addTarget:self action:@selector(goLoginVC:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

-(void)goLoginVC:(id)sender {
    NSLog(@"跳转界面");
}

scrollview和pageControl的通信,通过scrollview的代理方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //获取当前滑动到第几页
//    round()  C语言的四舍五入函数
    NSInteger currentPage = round(scrollView.contentOffset.x / scrollView.frame.size.width);

    _pageControl.currentPage = currentPage;

}

效果如下图:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值