你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS技巧綜合 >> RoateView

RoateView

編輯:IOS技巧綜合
[摘要]本文是對RoateView的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。
 #import <UIKit/UIKit.h>
 
 typedef void(^Click)(NSInteger index);
 
 @interface SLRoateView : UIView
 /*
  // tableView 中cell的復用機制始終沒整出來 所以現在只能放image 放tableview沒有意義 因為怎麼都要創建數組這麼多個tableview
 {
 // 當scrollView裝載的別的View 而不是imageView 比如一些App中scrollVeiw上放的的tableView 等等用以分類展示的view 像愛奇藝首頁的電影、電視、綜藝、動漫 這樣就不允許有輪播和點擊屬性
 
 // 是否允許輪播 默認允許 NO
 @property(nonatomic, assign)BOOL AllowCarousel;
 
 // 是否支持點擊 只需判斷block 是否為空 為空則不添加手勢
 @property(nonatomic, assign)BOOL SupportInteraction;
 }
 */
 // 創建
 + (SLRoateView *)roateViewWithFrame:(CGRect)frame array:(NSArray *)array click:(Click)click;
 
 //- (instancetype)initWithFrame:(CGRect)frame array:(NSArray *)array click:(Click)click;
 
 @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]];
        
    }
}
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved