OC-UIScrollView&UIPageControl

1.UIScorllView 滚动视图

作用:用于查看更大范围的图片或文字内容信息的视图
特点:虽然是视图,但本身没有外观,主要是通过记载子视图来完成内容的展示,内部子视图的区域可以大于scrollView占据的控件,通过滑动这种交互方法就能够实现查看超出范围的那些子视图
核心属性:
frame:视图的可见区域的大小
contentSeize:设置内容的大小
contentOffset:偏移量,记录滚动了多少

//scrollView的代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

//    NSInteger pageIndex = scrollView.contentOffset.x / scrollView.frame.size.width + 0.5;
    //round 四舍五入函数
    NSInteger pageIndex = round(scrollView.contentOffset.x / scrollView.frame.size.width);

    self.pageControl.currentPage = pageIndex;
}
[self.sv setContentOffset:CGPointMake(self.sv.contentOffset.x + 300,self.sv.contentOffset.y + 300) animated:YES];
//去掉导航栏前 半透明的图片
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];


UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background"]];


//创建滚动视图
UIScrollView *sv = [[UIScrollView alloc]init];
self.sv = sv;
sv.backgroundColor = [UIColor redColor];
//设置可见区域的大小
sv.frame = CGRectMake(0, 0, 200, 200);
//设置内容区域
sv.contentSize = imageView.frame.size;
//设置隐藏滑动时垂直方向滚动条
sv.showsVerticalScrollIndicator = NO;
//设置显示内容
[sv addSubview:imageView];

//方式一:关闭 控制器 有导航栏时自动调整滚动视图内容的功能
//self.automaticallyAdjustsScrollViewInsets = NO;
//方式二:调整内边距
sv.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);

2.UIPageControl

作用:页数的提醒
核心属性:
numberOfPages 圆点的个数
currentPage 当前被选中的是第几个小圆点

@interface MyViewController () <UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIPageControl *pageControl;
@end
-(void)setupPageControl {
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 60, self.view.frame.size.width, 40)];
    //需要4个圆点
    self.pageControl.numberOfPages = 4;
    //设置当前是 第几页
    self.pageControl.currentPage = 0;
    //设置没有选中的 圆点的颜色
    self.pageControl.pageIndicatorTintColor = [UIColor redColor];
    //设置选中的 圆点的颜色
    self.pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    //关闭用户交互
    self.pageControl.userInteractionEnabled = NO;

    [self.view addSubview:self.pageControl];
}

-(void)setupScrollView {
    self.scrollView = [[UIScrollView alloc]init];
    self.scrollView.delegate = self;
    self.scrollView.frame = self.view.frame;
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height);
    //向 scrollView中添加 要显示的内容
    for (NSInteger i = 0; i < 4; i++) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.frame = CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
        NSString *imageName = [NSString stringWithFormat:@"welcome%ld", i+1];
        imageView.image = [UIImage imageNamed:imageName];
        //将创建好的 imageView 添加到滚动视图上
        [self.scrollView addSubview:imageView];

        //如果是最后一个iamgeView
        if (i == 3) {
            UIButton *button = [self createButton:@selector(enterLoginVc:)];
            button.frame = CGRectMake((imageView.frame.size.width - 150) / 2, (imageView.frame.size.height - 60) / 2, 150, 60);
            //将button添加到最后一个imageView上
            [imageView addSubview:button];
            //imageView 默认是关闭用户交互的 那么button是imageView的子视图 也跟着关闭了,如果希望button可以点击,就要打开imageView的用户交互
            imageView.userInteractionEnabled = YES;
        }
    }
    //关闭弹跳
    self.scrollView.bounces = NO;

    //设置scrollView整页滑动
    self.scrollView.pagingEnabled = YES;

    //设置水平滚动条不可见
    self.scrollView.showsHorizontalScrollIndicator = NO;

    //将scrollView添加到 self.view 上
    [self.view addSubview:self.scrollView];
}

思考练习

制作一个图片单向无限滚动的sroll

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值