学習ノート---マルチキャスト図のパッケージ

5827 ワード

準備作業:cocopodsを利用してSDWebImage 1を導入する.CYView継承UIViewの作成
     
#define kInterval  2
//     block  
@property (nonatomic, copy) void(^imageBlock)(NSInteger index);
//        ,     frame        
- (instancetype)initWithFrame:(CGRect)frame imageURLs:(NSArray *)imageURLs;

2.SDヘッダファイル#importをインポートしてクラスの延長に書く
@interface CYView() 
//     scrollView
@property (nonatomic, strong) UIScrollView *scrollView;
//     
@property (nonatomic, strong) NSArray *imageURLs;
@property (nonatomic, strong) UIPageControl *pageController;
//                           
@property (nonatomic, strong) NSTimer *timer;
@end
//   frame        
-(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];
        //         timer
        if (self.imageURLs.count >= 2) {
            [self createTimer];
        }
    }
    return self;
}

ページングコントローラの作成
- (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;
}

スクロールビューの作成
- (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{
    //         (    )             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);
    //     
    self.scrollView.pagingEnabled = YES;
    //          
    self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    for (int i = 0; i < self.imageURLs.count + 2; i++) {  
    //          
        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;
        //             
        if (i == 0) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageURLs.lastObject]];
            //             
        } 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 {
    //   block
    if (self.imageBlock) {
        self.imageBlock(self.pageController.currentPage);
    }
}

スクロールプロトコル#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]];
}

//コードがオフセット量を変更してアニメーションを使用している場合、このメソッドを呼び出すと減速終了メソッドは呼び出されません.//-(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];
}