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 // 是否允许轮播 默认允许 NO
12 @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/