标签:
创建代码块:
YHView.m
1 #import "YHView.h" 2 #define kCount self.imgArr.count 3 #define kWidth self.frame.size.width 4 #define kHeight self.frame.size.height 5 6 @implementation YHView 7 8 - (instancetype)initWithFrame:(CGRect)frame 9 andImgArr:(NSMutableArray *)imgArr { 10 if (self = [super initWithFrame:frame]) { 11 self.imgArr = imgArr; 12 13 // 添加子视图 14 [self add]; 15 } 16 return self; 17 } 18 19 20 21 - (void)add { 22 23 // 创建scroll对象 24 self.scroll = [[UIScrollView alloc] initWithFrame:self.frame]; 25 self.scroll.contentSize = CGSizeMake(kWidth *(kCount + 2), 0); 26 self.scroll.pagingEnabled = YES; 27 self.scroll.showsHorizontalScrollIndicator = NO; 28 29 [self addSubview:self.scroll]; 30 31 // 将最后一张图片添加到第一的位置 32 [self addImageView:(int)(kCount - 1)]; 33 self.imgView.frame = CGRectMake(0, 0, kWidth, kHeight); 34 35 // for循环添加imgView 36 for (int i = 0; i < kCount; i++) { 37 [self addImageView:i]; 38 self.imgView.frame = CGRectMake(kWidth * (i + 1), 0, kWidth, kHeight); 39 } 40 41 // 将第一张放到最后的位置 42 [self addImageView:0]; 43 self.imgView.frame = CGRectMake(kWidth * (kCount + 1), 0, kWidth, kHeight); 44 45 // 设置初始偏移量 46 self.scroll.contentOffset = CGPointMake(kWidth, 0); 47 48 49 50 // 创建page对象 51 self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kHeight - 20, kWidth, 20)]; 52 53 self.page.numberOfPages = kCount; 54 55 [self addSubview:self.page]; 56 } 57 58 59 // 创建imgView添加到scroll上 60 - (void)addImageView:(int)index { 61 self.imgView = [[UIImageView alloc] initWithImage:self.imgArr[index]]; 62 [self.scroll addSubview:self.imgView]; 63 } 64 65 66 @end
根视图:
RootView.m
1 #import "RootView.h" 2 3 @implementation RootView 4 5 - (instancetype)initWithFrame:(CGRect)frame 6 { 7 self = [super initWithFrame:frame]; 8 if (self) { 9 [self add]; 10 } 11 return self; 12 } 13 14 15 - (void)add { 16 17 // 创建存放图片的数组 18 NSMutableArray *imgArr = [[NSMutableArray alloc] init]; 19 for (int i = 0; i < 10; i++) { 20 [imgArr addObject:[UIImage imageNamed:[NSString stringWithFormat:@"00%d.jpg", i]]]; 21 } 22 23 // 创建YHView对象 24 self.yhView = [[YHView alloc] initWithFrame:[UIScreen mainScreen].bounds andImgArr:imgArr]; 25 [self addSubview:self.yhView]; 26 } 27 28 @end
根视图控制器:
RootViewController.m
1 #import "RootViewController.h" 2 #import "RootView.h" 3 #define kCount self.rootView.yhView.imgArr.count 4 #define kWidth self.rootView.frame.size.width 5 #define kHeight self.rootView.frame.size.height 6 7 @interface RootViewController () <UIScrollViewDelegate> 8 @property (nonatomic, strong) RootView *rootView; 9 //@property (nonatomic, assign) int index; 10 11 @end 12 13 @implementation RootViewController 14 15 - (void)loadView { 16 self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 17 self.view = self.rootView; 18 19 //self.index = 0; 20 } 21 22 23 - (void)viewDidLoad { 24 [super viewDidLoad]; 25 26 self.rootView.yhView.scroll.delegate = self; 27 28 // 给page添加事件 29 [self.rootView.yhView.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged]; 30 31 // 添加计时器 32 [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; 33 34 } 35 36 37 // 实现代理方法 38 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 39 NSInteger temp = self.rootView.yhView.scroll.contentOffset.x / kWidth; 40 41 if (temp == 0) { 42 self.rootView.yhView.page.currentPage = kCount - 1; 43 self.rootView.yhView.scroll.contentOffset = CGPointMake(kCount * kWidth, 0); 44 } else if (temp > 0 && temp < kCount + 1) { 45 self.rootView.yhView.page.currentPage = temp - 1; 46 } else { 47 self.rootView.yhView.page.currentPage = 0; 48 self.rootView.yhView.scroll.contentOffset = CGPointMake(kWidth, 0); 49 } 50 51 } 52 53 54 // 实现page事件 55 - (void)pageAction:(UIPageControl *)page { 56 57 [self.rootView.yhView.scroll setContentOffset:CGPointMake(kWidth * (page.currentPage + 1), 0) animated:YES]; 58 } 59 60 61 // 实现计时器事件 62 - (void)timerAction:(NSTimer *)timer { 63 NSInteger temp = self.rootView.yhView.scroll.contentOffset.x / kWidth; 64 //self.index = (int)temp; 65 66 // 图片播到最后一张,无动画,将偏移量设置到第二张图片(也就是视觉上的第一张) 67 if (temp == kCount + 1) { 68 self.rootView.yhView.scroll.contentOffset = CGPointMake(kWidth, 0); 69 //self.index = 1; 70 temp = 1; 71 } 72 73 // 有动画,将图片滚动到第三张(也就是视觉上的第二张) 74 [self.rootView.yhView.scroll setContentOffset:CGPointMake(kWidth * (temp + 1), 0) animated:YES]; 75 76 // 设置小圆点位置 77 if (temp == kCount) { 78 self.rootView.yhView.page.currentPage = 0; 79 } else { 80 self.rootView.yhView.page.currentPage = temp; 81 } 82 } 83 84 85 @end
标签:
原文地址:http://www.cnblogs.com/zhizunbao/p/5414474.html