博客> UI学习第六天之UIScrollView
UI学习第六天之UIScrollView
2019-11-16 16:23 评论:2 阅读:1454 kingsuccess
ios 滚动视图 图片轮播

一:基本API

1、UIScrollView:滚动视图,核心是滚动和放大缩小

2、contentSize:决定内容区域的大小,内容区域的大小决定是横向滚动还是纵向滚动; bigScrollView.contentSize = CGSizeMake(414 * 4, 736);

3、// 设置缩小比例 scrollView.minimumZoomScale = 0.5; // 设置放大比例 scrollView.maximumZoomScale = 2;

@property(nonatomic) CGFloat zoomScale; 变化比例, 通常用本属性的setter方法, 让子视图完成实现相应的缩放比例

@property(nonatomic,readonly,getter=isZooming) BOOL zooming; 判断是否正在缩放状态

@property(nonatomic) BOOL bouncesZoom; 控制缩放的时候是否会反弹 默认YES

4、// 设置是否整屏翻动,默认是NO bigScrollView.pagingEnabled = YES;

5、// 设置边界是否回弹,默认是YES bigScrollView.bounces = NO;

6、// 点击状态栏是否可以回到顶部,默认是YES(纵向滚动) bigScrollView.scrollsToTop = YES;

7、// 设置偏移量 bigScrollView.contentOffset = CGPointMake(414 * 2, 0);

8、// 设置是否显示横向滚动条(默认是YES) bigScrollView.showsHorizontalScrollIndicator = NO;

//  设置是否显示纵向滚动条(默认是YES)
bigScrollView.showsVerticalScrollIndicator = NO;

9、UIPageControll 一般情况下不会自己的单独使用,只会和滚动视图结合使用(有addTarget方法)

9.1 // 设置小圆点的个数 page.numberOfPages = 4;

9.2// 设置当前小圆点的颜色 page.currentPageIndicatorTintColor = [UIColor redColor];

9.3 // 设置非当前小圆点的颜色 page.pageIndicatorTintColor = [UIColor whiteColor];

9.4 // 设置小圆点的初始值 page.currentPage = 2;

10、代理:UIScrollViewDelegate 创建4个小的子视图UIScrollView添加在父视图UIScrollView上;每一个子视图UIScrollView上各有一个UIImageView; 当实现图片的缩放功能时,是子视图的UIScrollView执行代理方法;当实现滚动功能时,是父视图的UIScrollView执行代理方法; // 设置代理 (实现滚动) bigScrollView.delegate = self; // 设置代理 (实现缩放) scrollView.delegate = self;

二:方法 1、允许哪些视图可以缩放

  • (UIView )viewForZoomingInScrollView:(UIScrollView )scrollView

2、 视图缩放时,自动调用的方法

  • (void)scrollViewDidZoom:(UIScrollView *)scrollView

3、开始滚动调用的代理方法

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView

4、开始拖拽调用的代理方法

  • (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

5、结束拖拽调用的代理方法

  • (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

6、开始减速调用的代理方法

  • (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView

7、结束减速调用的代理方法(一般将要实现的功能写在这个方法里面)

  • (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

三:实现图片轮播功能

UIScrollView scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; scrollView.backgroundColor = [UIColor redColor]; scrollView.contentSize = CGSizeMake(414 6, 736); scrollView.bounces = NO; scrollView.pagingEnabled = YES; scrollView.showsHorizontalScrollIndicator = NO; scrollView.showsVerticalScrollIndicator = NO; // 偏移量 scrollView.contentOffset = CGPointMake(414 * 1, 0); [self.view addSubview:scrollView]; [scrollView release]

for (int i = 0; i < 4; i++) {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(414 * (i + 1), 0, 414, 736)];
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"cywl_d.jpg",i + 1]];
    scrollView.delegate = self;
    [scrollView addSubview:imageView];
    [imageView release];

}

//
UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 414, 736)];
firstImageView.image = [UIImage imageNamed:@"cywl_04.jpg"];
[scrollView addSubview:firstImageView];
[firstImageView release];

//  在最右边添加第一张图

UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(414 * 5, 0, 414, 736)];
lastImageView.image = [UIImage imageNamed:@"cywl_01.jpg"];
[scrollView addSubview:lastImageView];
[lastImageView release];

UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 706, 414, 30)];
page.backgroundColor = [UIColor brownColor];
page.numberOfPages = 4;
[self.view addSubview:page];
[page release];

}

  • (void)scrollViewDidEndDecelerating:(UIScrollView )scrollView { // 接收当前所停止的图片是第几张 NSInteger index = scrollView.contentOffset.x / 414; UIPageControl page = self.view.subviews[1]; page.currentPage = index - 1; if (index == 5) { [scrollView setContentOffset:CGPointMake(414 1, 0) animated:NO]; page.currentPage = 0; } else if (index == 0) { [scrollView setContentOffset:CGPointMake(414 4, 0)]; page.currentPage = 3; }

}

收藏
2
sina weixin mail 回到顶部