學習筆記---輪播圖的封裝

準備工作:利用cocopods導入SDWebImage
1.創(chuàng)建一個CYView繼承UIView

 自動時間
#define kInterval  2
// 圖片點擊block回調(diào)
@property (nonatomic, copy) void(^imageBlock)(NSInteger index);
// 給外界留的接口,只需要傳入frame和裝有網(wǎng)址的數(shù)組
- (instancetype)initWithFrame:(CGRect)frame imageURLs:(NSArray *)imageURLs;

2.導入SD頭文件 #import <UIImageView+WebCache.h>
寫在類的延展里面

@interface CYView() <UIScrollViewDelegate>
// 最下面的scrollView
@property (nonatomic, strong) UIScrollView *scrollView;
// 網(wǎng)址數(shù)組
@property (nonatomic, strong) NSArray *imageURLs;
@property (nonatomic, strong) UIPageControl *pageController;
// 定時器 當用戶拖拽時 不需要自動滑動 所以設置為屬性
@property (nonatomic, strong) NSTimer *timer;
@end
// 傳入frame和裝有網(wǎng)址的數(shù)組
-(instancetype)initWithFrame:(CGRect)frame imageURLs:(NSArray *)imageURLs {
    self = [super initWithFrame:frame];
    if (self) {
        if (imageURLs.count == 0) {
            return nil;
        }
        self.imageURLs = imageURLs;
        [self createScrollView];
        [self initImages];
        [self createPageController];
        // 兩張圖以上才創(chuàng)建timer
        if (self.imageURLs.count >= 2) {
            [self createTimer];
        }
    }
    return self;
}

創(chuàng)建分頁控制器

- (void)createPageController {
    self.pageController = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width/2 - 60, self.frame.size.height - 20, 120, 20)];
    self.pageController.pageIndicatorTintColor = [UIColor greenColor];
    self.pageController.currentPageIndicatorTintColor = [UIColor redColor];
    [self addSubview:self.pageController];
    // 一共幾個diandian
    self.pageController.numberOfPages = self.imageURLs.count;
}

創(chuàng)建滾動視圖

- (void)createScrollView {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.scrollView.contentSize = CGSizeMake(self.frame.size.width *  self.imageURLs.count, 0);
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.scrollView];  
}

初始化圖片

- (void)initImages{
    // 如果只有一個網(wǎng)址(一張圖片)不用循環(huán)輪播 直接創(chuàng)建一張imageView
    if (self.imageURLs.count == 1) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageURLs[0]]];
        [self.scrollView addSubview:imageView];        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFunction)];
        // 給每一張圖片都添加一個點擊事件
        [imageView addGestureRecognizer:tap];
        // 打開imageView交互
        imageView.userInteractionEnabled = YES;        
        return;
    }    
    // 在左右兩邊各添加一張imageView
    self.scrollView.contentSize = CGSizeMake(self.frame.size.width * (self.imageURLs.count + 2), 0);
    // 整夜?jié)L動
    self.scrollView.pagingEnabled = YES;
    // 一上來就顯示第一個
    self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    for (int i = 0; i < self.imageURLs.count + 2; i++) {  
    //創(chuàng)建手勢      
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFunction)];        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        // 給每一張圖片都添加一個點擊事件
        [imageView addGestureRecognizer:tap];
        // 打開imageView交互
        imageView.userInteractionEnabled = YES;
        // 第一張顯示最后一張的內(nèi)容
        if (i == 0) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageURLs.lastObject]];
            // 最后一張顯示第一張的內(nèi)容
        } else if (i == self.imageURLs.count + 1) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageURLs.firstObject]];
        } else {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageURLs[i - 1]]];
        }
        [self.scrollView addSubview:imageView];
    }
}

圖片點擊

- (void)imageClickFunction {
    // 回調(diào)block
    if (self.imageBlock) {
        self.imageBlock(self.pageController.currentPage);
    }
}

實現(xiàn)滾動協(xié)議 #pragma mark ---UIScrollViewDelegate---

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 如果偏移到第0個 回到最后一個
    if (self.scrollView.contentOffset.x <= 0) {
        self.scrollView.contentOffset = CGPointMake(self.imageURLs.count * self.frame.size.width, 0);
    }
    if (self.scrollView.contentOffset.x >= (self.imageURLs.count + 1) * self.frame.size.width) {
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    self.pageController.currentPage = self.scrollView.contentOffset.x / self.frame.size.width - 1;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    // 定時器沒有提供暫停的方法  我們可以設置他在什么時間開啟
    [self.timer setFireDate:[NSDate distantFuture]];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // [self createTimer];
    [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:kInterval]];
}

// 當我們代碼改變了偏移量 并且使用了動畫的時候 會調(diào)用這個方法 不會調(diào)用結束減速的方法,可以手動調(diào)用
//- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
// [self scrollViewDidEndDecelerating:self.scrollView];
//}
定時器

- (void)createTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeHander) userInfo:nil repeats:YES];
}

// 定時器方法

- (void)timeHander {
    CGPoint offSet = self.scrollView.contentOffset;
    offSet.x += self.frame.size.width;
    [self.scrollView setContentOffset:offSet animated:YES];
}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容