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)系作者
  • 序言:七十年代末吉殃,一起剝皮案震驚了整個(gè)濱河市衰琐,隨后出現(xiàn)的幾起案子宏娄,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件禁谦,死亡現(xiàn)場離奇詭異胁黑,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)州泊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來漂洋,“玉大人遥皂,你說我怎么就攤上這事」羝” “怎么了演训?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長贝咙。 經(jīng)常有香客問我样悟,道長,這世上最難降的妖魔是什么庭猩? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任窟她,我火速辦了婚禮,結(jié)果婚禮上蔼水,老公的妹妹穿的比我還像新娘震糖。我一直安慰自己,他們只是感情好趴腋,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布吊说。 她就那樣靜靜地躺著,像睡著了一般优炬。 火紅的嫁衣襯著肌膚如雪颁井。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天蠢护,我揣著相機(jī)與錄音雅宾,去河邊找鬼。 笑死糊余,一個(gè)胖子當(dāng)著我的面吹牛秀又,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播贬芥,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼吐辙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蘸劈?” 一聲冷哼從身側(cè)響起昏苏,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后贤惯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體洼专,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年孵构,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了屁商。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,021評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡颈墅,死狀恐怖蜡镶,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情恤筛,我是刑警寧澤官还,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站毒坛,受9級特大地震影響望伦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜煎殷,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一屯伞、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蝌数,春花似錦愕掏、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至唆貌,卻和暖如春滑潘,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背锨咙。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工语卤, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人酪刀。 一個(gè)月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓粹舵,卻偏偏與公主長得像,于是被迫代替她去往敵國和親骂倘。 傳聞我的和親對象是個(gè)殘疾皇子眼滤,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,974評論 2 355

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

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