【转】 iOS开发UI篇—UIScrollView控件实现图片轮播

原文:http://www.cnblogs.com/wendingding/p/3763527.html

iOS开发UI篇—UIScrollView控件实现图片轮播

一、实现效果

实现图片的自动轮播

          

二、实现代码

storyboard中布局

代码:

  1 #import "YYViewController.h"
  2 
  3 @interface YYViewController () <UIScrollViewDelegate>
  4 @property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
  5 /**
  6  *  页码
  7  */
  8 @property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
  9 
 10 @property (nonatomic, strong) NSTimer *timer;
 11 @end
 12 
 13 @implementation YYViewController
 14 
 15 - (void)viewDidLoad
 16 {
 17     [super viewDidLoad];
 18     
 19 //    图片的宽
 20     CGFloat imageW = self.scrollview.frame.size.width;
 21 //    CGFloat imageW = 300;
 22 //    图片高
 23     CGFloat imageH = self.scrollview.frame.size.height;
 24 //    图片的Y
 25     CGFloat imageY = 0;
 26 //    图片中数
 27     NSInteger totalCount = 5;
 28 //   1.添加5张图片
 29     for (int i = 0; i < totalCount; i++) {
 30         UIImageView *imageView = [[UIImageView alloc] init];
 31 //        图片X
 32         CGFloat imageX = i * imageW;
 33 //        设置frame
 34         imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
 35 //        设置图片
 36         NSString *name = [NSString stringWithFormat:@"img_0%d", i + 1];
 37         imageView.image = [UIImage imageNamed:name];
 38 //        隐藏指示条
 39         self.scrollview.showsHorizontalScrollIndicator = NO;
 40         
 41         [self.scrollview addSubview:imageView];
 42     }
 43     
 44 //    2.设置scrollview的滚动范围
 45     CGFloat contentW = totalCount *imageW;
 46     //不允许在垂直方向上进行滚动
 47     self.scrollview.contentSize = CGSizeMake(contentW, 0);
 48     
 49 //    3.设置分页
 50     self.scrollview.pagingEnabled = YES;
 51     
 52 //    4.监听scrollview的滚动
 53     self.scrollview.delegate = self;
 54     
 55     [self addTimer];
 56 }
 57 
 58 - (void)nextImage
 59 {
 60     int page = (int)self.pageControl.currentPage;
 61     if (page == 4) {
 62         page = 0;
 63     }else
 64     {
 65         page++;
 66     }
 67     
 68 //  滚动scrollview
 69     CGFloat x = page * self.scrollview.frame.size.width;
 70     self.scrollview.contentOffset = CGPointMake(x, 0);
 71 }
 72 
 73 // scrollview滚动的时候调用
 74 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
 75 {
 76     NSLog(@"滚动中");
 77 //    计算页码
 78 //    页码 = (contentoffset.x + scrollView一半宽度)/scrollView宽度
 79     CGFloat scrollviewW =  scrollView.frame.size.width;
 80     CGFloat x = scrollView.contentOffset.x;
 81     int page = (x + scrollviewW / 2) /  scrollviewW;
 82     self.pageControl.currentPage = page;
 83 }
 84 
 85 // 开始拖拽的时候调用
 86 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
 87 {
 88 //    关闭定时器(注意点; 定时器一旦被关闭,无法再开启)
 89 //    [self.timer invalidate];
 90     [self removeTimer];
 91 }
 92 
 93 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
 94 {
 95 //    开启定时器
 96     [self addTimer];
 97 }
 98 
 99 /**
100  *  开启定时器
101  */
102 - (void)addTimer{
103     
104     self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
105 106 }
106 /**
107  *  关闭定时器
108  */
109 - (void)removeTimer
110 {
111     [self.timer invalidate];
112 }
113 @end

 


补充:
1.先介绍下UIScrollView的常见属性                    

1 @property(nonatomic) CGPoint contentOffset; // 记录UIScrollView滚动的位置
2 @property(nonatomic) CGSize contentSize; // 内容尺寸(能滚动的范围)
3 @property(nonatomic) UIEdgeInsets contentInset; // 额外增加的滚动区域(在上下左右4个边缘)
4 @property(nonatomic,assign) id<UIScrollViewDelegate> delegate; // 代理对象
5 @property(nonatomic) BOOL bounces; // 是否有弹簧效果
6 @property(nonatomic) BOOL showsHorizontalScrollIndicator; // 是否显示水平滚动条
7 @property(nonatomic) BOOL showsVerticalScrollIndicator; // 是否显示垂直滚动条


2.分页浏览的实现                            

2.1.把需要显示的图片设置进UIScrollView 
 
1 CGFloat w = self.view.frame.size.width;
2 CGFloat h = self.view.frame.size.height;
3 for (int i = 0; i< kCount; i++) {
4 UIImageView *imageView = [[UIImageView alloc] init];
5 
6 // 1.设置frame
7 imageView.frame = CGRectMake(i * w, 0, w, h);
8 
9 // 2.设置图片
10 NSString *imgName = [NSString stringWithFormat:@"0%d.jpg", i + 1];
11 imageView.image = [UIImage imageNamed:imgName];
12 
13 [_scrollView addSubview:imageView];

 


2.2.设置UIScrollView的相关属性                            


属性在文章的开头有介绍

1 // height == 0 代表 禁止垂直方向滚动
2 _scrollView.contentSize = CGSizeMake(kCount * w, 0);
3 _scrollView.showsHorizontalScrollIndicator = NO;
4 _scrollView.pagingEnabled = YES;
5 _scrollView.delegate = self;

 


2.3.设置UIPageControl的相关属性,计算页码                            

 

 1 UIPageControl *pageControl = [[UIPageControl alloc] init];
 2 pageControl.center = CGPointMake(w * 0.5, h - 20);
 3 pageControl.bounds = CGRectMake(0, 0, 150, 50);
 4 pageControl.numberOfPages = kCount; // 一共显示多少个圆点(多少页)
 5 // 设置非选中页的圆点颜色
 6 pageControl.pageIndicatorTintColor = [UIColor redColor];
 7 // 设置选中页的圆点颜色
 8 pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
 9 
10 // 禁止默认的点击功能
11 pageControl.enabled = NO;
12 
13 [self.view addSubview:pageControl];
14 _pageControl = pageControl;

 

 


2.4.滚动时切换页码                                         

 1 #pragma mark - UIScrollView的代理方法
 2 #pragma mark 当scrollView正在滚动的时候调用
 3 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
 4 {
 5 int page = scrollView.contentOffset.x / scrollView.frame.size.width;
 6 // NSLog(@"%d", page);
 7 
 8 // 设置页码
 9 _pageControl.currentPage = page;
10 }

 

                           

posted on 2015-08-14 14:22  MichaelMao  阅读(239)  评论(0编辑  收藏  举报