设为首页 加入收藏

TOP

RoateView(一)
2017-10-13 10:17:20 】 浏览:2279
Tags:RoateView
 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/
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇KVO——下拉改变导航栏透明度 下一篇iOS开发 GET、POST请求方法(NSUR..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目