iOS-UIScrollerView

2023-11-22 09:07:00 浏览数 (1)

UIScrollerView常见属性
  • CGPoint contentOffset 这个属性用来表示UIScrollerView滚动的位置(其实就是内容左上角与ScrollerView左上角的间距值)
  • CGSize contentSize 这个属性用来表示UIScrollerView内容尺寸,滚动范围(能滚多远)
  • UIEdgeInsets contentInset 这个属性能够在UIScrollerView的四周增加额外的滚动区域,一般用来避免scrollerView的内容被其他控件挡住
UIScrollerView无法滚动的原因
  • 没有设置contentSize
  • scrollEnabled = NO
  • 没有收到触摸事件:ueserInteractionEnabled = NO
UIScrollerView代理方法
  • ScrollerView滑动的代理回调
代码语言:javascript复制
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"即将开始滑动内容时");
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"正在滑动");
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    NSLog(@"当用户完成滑动内容时");
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"结束滑动");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"将开始减速");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"减速完毕,停止滑动");
}
  • ScrollerView缩放的代理回调 必须设置scrollerView的缩放比例 scrollerView.maximumZoomScale = 2.0; scrollerView.minimumZoomScale = 0.2;
代码语言:javascript复制
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view{
    NSLog(@"开始缩放");
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    NSLog(@"缩放");
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale{
    NSLog(@"结束缩放");
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return [UIView new];
}
  • ScrollerView缩放的代理回调
代码语言:javascript复制
//滚动视图在和方法的实现结束时调用此方法,但仅在请求动画时调用setContentOffset:animated:scrollRectToVisible:animated:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    NSLog(@"结束缩放动画");
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
    NSLog(@"点击状态栏是否可以回到顶部");
    return YES;
}
//scrollViewShouldScrollToTop设置为YES,滚动视图在完成滚动到内容顶部时发送此消息
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
    NSLog(@"滚动视图在完成滚动到内容顶部时发送此消息");
}
UIScrollerView分页(电商与新闻常用)

简单示例.gif

代码语言:javascript复制
@interface ScrollViewPage ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UIScrollView *scrollerView;
@property (nonatomic,strong) UIPageControl *pageControl;
@end
@implementation ScrollViewPage
- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 230)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    [self loadScrollViewCon:view];
    [self loadPageCon:view];
}

- (void)loadScrollViewCon:(UIView *)view{
    self.scrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, view.bounds.size.width, 230)];
    self.scrollerView.delegate = self;
    [self.scrollerView setShowsVerticalScrollIndicator:NO];
    [self.scrollerView setShowsHorizontalScrollIndicator:NO];

    CGFloat w = self.scrollerView.bounds.size.width;
    CGFloat h = self.scrollerView.bounds.size.height;
    NSInteger pageCount = 5;
    for (int i = 0; i < pageCount; i  ) {
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"pic%d",i]]];
        [imageView setFrame:CGRectMake(w*i, 0, w, h)];
        [self.scrollerView addSubview: imageView];
    }

    //设置内容视图大小
    [self.scrollerView setContentSize:CGSizeMake(w*pageCount, 0)];
    //开启分页
    [self.scrollerView setPagingEnabled:YES];
    [view addSubview:self.scrollerView];
}
- (void)loadPageCon:(UIView *)view{
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(view.bounds.size.width-200, view.bounds.size.height-20, 200, 20)];
    self.pageControl.numberOfPages = 5;
    self.pageControl.pageIndicatorTintColor = [UIColor orangeColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
    [view addSubview:self.pageControl];
}

#pragma mark- scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    //0.3 > (int)(0.3 0.5) > 0
    //0.3 > (int)(0.3 0.5) > 0
    //小数四舍五入为整数:(int)(小数   0.5)
    NSInteger page = (int)(self.scrollerView.contentOffset.x / self.view.frame.size.width   0.5);
    self.pageControl.currentPage = page;
    NSLog(@"%zd",page);
}
@end

0 人点赞