RoateView,createview
1 #import <UIKit/UIKit.h> 2 3 typedef void(^Click)(NSInteger index); 4 5 @interface SLRoateView : UIView 6 /* 7 // tableView 中cell的複用機制始終沒整出來 所以現在只能放image 放tableview沒有意義 因為怎麼都要建立數組這麼多個tableview 8 { 9 // 當scrollView裝載的別的View 而不是imageView 比如一些App中scrollVeiw上放的的tableView 等等用以分類展示的view 像愛奇藝首頁的電影、電視、綜藝、動漫 這樣就不允許有輪播和點擊屬性10 11 // 是否允許輪播 預設允許 NO12 @property(nonatomic, assign)BOOL AllowCarousel;13 14 // 是否支援點擊 只需判斷block 是否為空白 為空白則不添加手勢15 @property(nonatomic, assign)BOOL SupportInteraction;16 }17 */18 // 建立19 + (SLRoateView *)roateViewWithFrame:(CGRect)frame array:(NSArray *)array click:(Click)click;20 21 //- (instancetype)initWithFrame:(CGRect)frame array:(NSArray *)array click:(Click)click;22 23 @end
#import "SLRoateView.h"@interface SLRoateView ()<UIScrollViewDelegate>@property(nonatomic, strong)NSArray *array;@property(nonatomic, copy)Click click;@property(nonatomic, assign)CGFloat KRoateWidth;@property(nonatomic, assign)CGFloat KRoateHeight;@property(nonatomic, strong)UIImageView *leftImageView;@property(nonatomic, strong)UIImageView *centerImageView;@property(nonatomic, strong)UIImageView *rightImageView;@property(nonatomic, assign)NSInteger index;@property(nonatomic, strong)UIPageControl *pageControl;@property(nonatomic, strong)NSTimer *timer;@property(nonatomic, strong)UIScrollView *scroll;@end@implementation SLRoateView+ (SLRoateView *)roateViewWithFrame:(CGRect)frame array:(NSArray *)array click:(Click)click{ SLRoateView *roateView = [[SLRoateView alloc]initWithFrame:frame array:array click:click]; return roateView;}- (instancetype)initWithFrame:(CGRect)frame array:(NSArray *)array click:(Click)click{ self = [super initWithFrame:frame]; if (self) { self.KRoateWidth = frame.size.width; self.KRoateHeight = frame.size.height; self.array = array; self.click = click; // scrollVeiw 設定 self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _KRoateWidth, _KRoateHeight)]; self.scroll.contentSize = CGSizeMake(_KRoateWidth * 3, 0); self.scroll.pagingEnabled = YES; self.scroll.contentOffset = CGPointMake(_KRoateWidth, 0); self.scroll.alwaysBounceVertical = NO; self.scroll.showsVerticalScrollIndicator = NO; self.scroll.alwaysBounceHorizontal = YES; self.scroll.showsHorizontalScrollIndicator = NO; self.scroll.delegate = self; [self addSubview:_scroll]; [self addGestureRecognizer]; [self addImageViews]; [self setDefautImage]; [self addPageControl]; [self addTimer]; } return self;}#pragma mark 添加計時器- (void)addTimer{ // 在子線程中建立一個計時器 是圖片實現輪播 dispatch_async(dispatch_get_global_queue(0, 0), ^{ _timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(rotaView) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; [[NSRunLoop currentRunLoop] run]; });}- (void)rotaView{ [_scroll setContentOffset:CGPointMake(_KRoateWidth * 2, 0) animated:YES]; NSLog(@"%ld", _index); // 設定分頁 由於動畫是從中間移動到第三張 所以pageControl要顯示的不是中間圖片而是第三張圖片 _pageControl.currentPage = (_index + 1) % _array.count; if (_scroll.contentOffset.x/_KRoateWidth == 2) { [self reloadImage]; // scrollView 回到中間 [self.scroll setContentOffset:CGPointMake(_KRoateWidth, 0) animated:NO]; }}#pragma mark 添加點擊事件- (void)addGestureRecognizer{ [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureRecognizer)]];}- (void)tapGestureRecognizer{ if (_click) { // 用當前的 self.click(_pageControl.currentPage); } }#pragma mark 添加圖片控制項- (void)addImageViews{ _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _KRoateWidth, _KRoateHeight)]; // imageView 大小不會改變 UIViewContentModeScaleAspectFit 只是image大小變化了// _leftImageView.contentMode = UIViewContentModeScaleAspectFit; [self.scroll addSubview:_leftImageView]; _centerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_KRoateWidth, 0, _KRoateWidth, _KRoateHeight)];// _centerImageView.contentMode = UIViewContentModeScaleAspectFit; [self.scroll addSubview:_centerImageView]; _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_KRoateWidth * 2, 0, _KRoateWidth, _KRoateHeight)];// _rightImageView.contentMode = UIViewContentModeScaleAspectFit; [self.scroll addSubview:_rightImageView]; NSLog(@" %f %f %f", _leftImageView.frame.size.height, _centerImageView.frame.size.height, _rightImageView.frame.size.height);}#pragma mark 設定預設顯示圖片- (void)setDefautImage{ _leftImageView.image = _array[_array.count - 1]; _centerImageView.image = _array[0]; _rightImageView.image = _array[1]; _index = 0; _pageControl.currentPage = _index;}#pragma mark 添加分頁控制項- (void)addPageControl{ // 根據頁數放回UIPageControl合適的大小 返回(0,0)不知道為啥 ----先要建立出來再給frame _pageControl = [[UIPageControl alloc]init]; CGSize size = [_pageControl sizeForNumberOfPages:_array.count]; _pageControl.frame = CGRectMake(0, 0, size.width, size.height); _pageControl.center = CGPointMake(_KRoateWidth / 2, _KRoateHeight - size.height/2); _pageControl.numberOfPages = _array.count; [self addSubview:_pageControl];}//#pragma mark 拖拽事件//- (void)scrollViewDidScroll:(UIScrollView *)scrollView//{// if (scrollView.isDragging)// {// NSLog(@"關閉定時器");// [_timer setFireDate:[NSDate distantFuture]];// }// if (!scrollView.isDragging)// {// NSLog(@"asdfasdf");// }//}#pragma mark 滾動停止事件- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ // 5秒後啟動計時器 [_timer performSelector:@selector(setFireDate:) withObject:[NSDate distantPast] afterDelay:5]; [self reloadImage]; // scrollView 回到中間 [self.scroll setContentOffset:CGPointMake(_KRoateWidth, 0) animated:NO]; // 設定分頁 _pageControl.currentPage = _index; }#pragma mark 重新載入圖片- (void)reloadImage{ NSInteger leftIndex, rigtIndex; CGPoint offset = self.scroll.contentOffset; if (offset.x > _KRoateWidth) { _index = (_index + 1) % _array.count; }else if (offset.x < _KRoateWidth) { _index = (_index + _array.count - 1) % _array.count; } _centerImageView.image = _array[_index]; leftIndex = (_index + _array.count - 1) % _array.count; rigtIndex = (_index + 1) % _array.count; _leftImageView.image = _array[leftIndex]; _rightImageView.image = _array[rigtIndex];}- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ if (scrollView.isDragging) { // 取消timer之前的執行請求 [NSObject cancelPreviousPerformRequestsWithTarget:_timer]; // 關閉定時器 [_timer setFireDate:[NSDate distantFuture]]; }}