6.9 UIScrollView

UIScrollView
* 1、如何创建一个滚动视图,实现一个app的用户指导界面的展示;
* * 2、如何设置一个滚动视图的上下及左右延伸

   @property(nonatomic, strong)UIScrollView        *scollView; //滚动视图

    //0.设置自动调整scollview边界值
    self.automaticallyAdjustsScrollViewInsets = NO;
    //1,创建scollview
    self.scollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    //2, 获取滚动视图的宽高
    CGFloat width = CGRectGetWidth(self.scollView.bounds);
    CGFloat height = CGRectGetHeight(self.scollView.bounds);
    //设置属性
    //2.1 设置内容大小
    _scollView.contentSize = CGSizeMake(5 * width, height);

    //2.2添加分页效果
    _scollView.pagingEnabled = YES;
    //2.3设置横向指示条是否显示
    _scollView.showsHorizontalScrollIndicator = YES;
    //2.4设置纵向指示条是否显示
    _scollView.showsVerticalScrollIndicator = YES;
    //2.5设置代理
    _scollView.delegate = self;
  • 3、考虑相册的集成,左右滑动视图,循环显示相册中的照片(循环滚动效果)
  • UIPageControl
  • 1、结合UIScrollView,配置UIPageControl,实现联动效果

// 循环滚动步骤
// 1,改变UIImageView的数量(3),并且内容视图为3 * width
// 2,设置scrollView的偏移量(width,0)
// 3,创建imageView数组,用以获取imageView
// 4,根据下标动态的设置正确的视图

#pragma mark - UIScollViewDelegate方法
    //3 关联子视图
    for (int i = 0; i <self.imageArray.count; i ++) {
        //3.1创建imageview
        UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(i * width, 0, width, height)];
        //3.2关联图片
        imageview.image = self.imageArray[i];
        //3.3添加到scollview上
        [_scollView addSubview:imageview];
        //3.4设置图片缩放方式
        imageview.contentMode = UIViewContentModeScaleAspectFit;
    }

    //4,添加到父视图
    [self.view addSubview:_scollView];


}
#pragma mark - UIScollViewDelegate方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);
    //偏移量 《=0说明用户滑动界面在左边的位置
    //偏移量  》=2 * width说明用户滑动界面在右边位置

    if (scrollView.contentOffset.x <= 0) {
        [self pageLeft];

    }else if (scrollView.contentOffset.x >= 2 * CGRectGetWidth(scrollView.bounds)) {
        [self pageRight];
    }

}

#pragma mark - 循环滚动逻辑
- (void)pageLeft
{
    _currentIndex = (--_currentIndex + self.imageArray.count) % self.imageArray.count;

    [self dynamicLoadingImage];

}

- (void)pageRight
{
    _currentIndex = (++_currentIndex + self.imageArray.count) % self.imageArray.count;
    [self dynamicLoadingImage];
}

- (void)dynamicLoadingImage
{
    //1,设置当前pageControl下标
    self.pageControl.currentPage = self.currentIndex;
    //2, 循环配置图片
    for (int i = -1; i < 1; i ++) {
        //获取当前正确的下标
        // 7:(-1+8) % 8;    0:(8 % 8)  1:(1 + 8) % 8
        NSInteger index = (_currentIndex + i + _imageArray.count) % _imageArray.count;
        //取出相应地imageview
        UIImageView *imageView = self.imageViewArray[i + 1];
        //进行图片关联
        imageView.image = self.imageArray[index];
    }
    // 将偏移量返回
    [self.scollView setContentOffset:CGPointMake(CGRectGetWidth(_scollView.bounds), 0) animated:NO];

}
#pragma mark - UIScollView方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
     NSLog(@"%s",__func__);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
     NSLog(@"%s",__func__);
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
     NSLog(@"%s",__func__);
}
  • 2、改变UIPageControl的颜色
    // 5. 创建pageControl
    self.pageControl = [[UIPageControl alloc]init];
    // 5.1 设置大小
    _pageControl.bounds = CGRectMake(0, 0, 300, 30);
    // 5.2 设置中心点
    _pageControl.center = CGPointMake(width / 2, 450);
    // 5.2 设置背景颜色
    _pageControl.pageIndicatorTintColor = [UIColor redColor];
    // 5.3 设置当前指示器颜色
    _pageControl.currentPageIndicatorTintColor = [UIColor purpleColor];
    // 5.4 当前数量
    _pageControl.numberOfPages = self.imageArray.count;
    // 5.5 设置当前下标
    _pageControl.currentPage =self.currentIndex;
    // 5.6 添加到父视图
    [self.view addSubview:_pageControl];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值