UIScrollowView 使用三個(gè)UIimageView實(shí)現(xiàn)輪播效果

百度云地址鏈接 #密碼:3noo


需要調(diào)用的頁面

#import "ViewController.h"
#import "SeliceInfininiteScrolloView.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //實(shí)現(xiàn)調(diào)用方法 將圖片數(shù)組傳入
    SeliceInfininiteScrolloView *scrollowView =  [[SeliceInfininiteScrolloView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 200)];
    scrollowView.imageArray  = @[@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",@"6.png"];
    [self.view addSubview:scrollowView];
}

@end

自定義方法 SeliceInfininiteScrolloView.h


#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface SeliceInfininiteScrolloView : UIView
@property(nonatomic,strong) NSArray * imageArray;//圖片數(shù)組


@end

NS_ASSUME_NONNULL_END

自定義方法 SeliceInfininiteScrolloView.m

#import "SeliceInfininiteScrolloView.h"

@interface SeliceInfininiteScrolloView()<UIScrollViewDelegate>{
    
    UIScrollView    *MainScrollowView;
    UIImageView     *leftImageView;
    UIImageView     *MidImageView;
    UIImageView     *RightImageView;
    NSTimer         *timer;
    UIPageControl   *pageControl;
    NSInteger       currentIndex;
    NSInteger       count;
    
}

@end



@implementation SeliceInfininiteScrolloView

static const int viewNumber  =  3;


- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        currentIndex = 0;
    }
    
    return self;
}

-(void)setImageArray:(NSArray *)imageArray{
    
    _imageArray = imageArray;
    count       = imageArray.count;

    //    新建UIScrollowView
    [self creatScrollowView];
    //    新建定時(shí)器
    [self creatTimer];
    //    新建UIPageControl
    [self creatPagecontrol];
    
}


#pragma mark  新建UIScrollowView
-(void)creatScrollowView{

    //  當(dāng)你滑動(dòng)的時(shí)候,不滑動(dòng)出scrollowView的時(shí)候 那么是不是就不會(huì)出現(xiàn)紅色背景
    MainScrollowView = [[UIScrollView alloc]initWithFrame:self.bounds];
    MainScrollowView.pagingEnabled = YES;
    //   背景顏色的出現(xiàn),本質(zhì)是:滑動(dòng)過界了,超過了scrollowView的contentsize所以才會(huì)出現(xiàn)背景色
    MainScrollowView.bounces = NO;
    
    MainScrollowView.backgroundColor = [UIColor redColor];
    MainScrollowView.showsVerticalScrollIndicator = NO;
    MainScrollowView.showsHorizontalScrollIndicator = NO;
    MainScrollowView.delegate = self;
    MainScrollowView.contentSize = CGSizeMake(viewNumber*self.frame.size.width, self.frame.size.height);
    [self addSubview:MainScrollowView];
    
//   添加圖片
    leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    leftImageView.image = [UIImage imageNamed:_imageArray[count-1]];
    
    
    MidImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
    MidImageView.image = [UIImage imageNamed:_imageArray[0]];

    
    RightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(2*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
    RightImageView.image = [UIImage imageNamed:_imageArray[1]];

    [MainScrollowView addSubview:leftImageView];
    [MainScrollowView addSubview:MidImageView];
    [MainScrollowView addSubview:RightImageView];

//    剛剛開始的時(shí)候設(shè)置偏移量
    MainScrollowView.contentOffset = CGPointMake(self.frame.size.width, 0.f);
    
}

#pragma mark  新建定時(shí)器
-(void)creatTimer{
    
    __weak typeof(self)weakSelf = self;
    if (@available(iOS 10.0, *)) {
        timer = [NSTimer timerWithTimeInterval:2.f repeats:YES block:^(NSTimer * _Nonnull timer) {
            [weakSelf timerAction];
            
        }];
    } else {
        
    }
    
    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}


-(void)timerAction{
    
    [MainScrollowView scrollRectToVisible:CGRectMake(2*self.frame.size.width, 0.f, self.frame.size.width, self.frame.size.height) animated:YES];
    
}

-(void)invalidateTimer{
    [timer invalidate];
    timer = nil;
}


#pragma mark  新建UIPageControl
-(void)creatPagecontrol{
    
    CGFloat pageControlHeight = 20.f;
    CGFloat pageControlWidth  = 80.f;
    
    pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(20, self.frame.size.height-pageControlHeight, pageControlWidth, pageControlHeight)];
    pageControl.numberOfPages   = count;
    pageControl.currentPage     = 0.f;
    pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
    [self addSubview:pageControl];
    
}


#pragma mark  UIScrollView delegate method

//手動(dòng)滑動(dòng)停止減速的時(shí)候調(diào)用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == 2 * self.frame.size.width) {
        //滑動(dòng)到最右邊時(shí)候
        currentIndex ++;
        //重置圖片內(nèi)容 修改偏移量
        [self resetImages];
    }else if (scrollView.contentOffset.x==0){
       
        currentIndex = currentIndex + count;
        currentIndex --;
        [self resetImages];
    }
}


//定時(shí)器滑動(dòng)調(diào)用
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
 
    if (scrollView.contentOffset.x ==2*self.frame.size.width) {
        //滑動(dòng)到最右邊時(shí)候
        currentIndex ++;
        //重置圖片內(nèi)容 修改偏移量
        [self resetImages];
    }
}

#pragma mark  //重置圖片內(nèi)容 修改偏移量
-(void)resetImages{
 
    leftImageView.image  = [UIImage imageNamed:_imageArray[(currentIndex-1)%count]];
    MidImageView.image   = [UIImage imageNamed:_imageArray[(currentIndex)%count]];
    RightImageView.image = [UIImage imageNamed:_imageArray[(currentIndex+1)%count]];
    MainScrollowView.contentOffset = CGPointMake(self.frame.size.width, 0.f);
    
    pageControl.currentPage = (currentIndex)%count;//設(shè)置page控件當(dāng)前位置
    
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
 
    //準(zhǔn)備拖動(dòng)的時(shí)候,定時(shí)器失效
    [self invalidateTimer];
    
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
     //停止拖動(dòng)的時(shí)候,定時(shí)器開啟
    [self creatTimer];
}

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,304評論 4 61
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,653評論 1 32
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,815評論 1 45
  • 11月8日晚自習(xí)是我們飛翼班的演講課時(shí)間,今天我們的演講主題是“悅讀書·越成長”。(小插曲:剛開始的時(shí)候,以為是“...
    改變自己369閱讀 888評論 0 7
  • 春天里總會(huì)有很多美好的事情發(fā)生 老家的春天 四季分明 立春了還會(huì)冷上一段時(shí)間 每天騎車在上班的路上會(huì)看到路兩旁的垂...
    Roy__x閱讀 379評論 0 0

友情鏈接更多精彩內(nèi)容