iOS使用UIScrollView實現(xiàn)無限循環(huán)輪播圖效果
更新時間:2018年07月22日 09:09:15 作者:Leemin_ios
這篇文章主要介紹了iOS使用UIScrollView實現(xiàn)無限循環(huán)輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了iOS使用UIScrollView實現(xiàn)無限循環(huán)輪播圖的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
// // ViewController.m // 無限輪播 // // Created by limin on 17/8/23. // Copyright © 2017年 none. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate> /* 定時器 */ @property(nonatomic,strong)NSTimer *rotateTimer; /* */ @property(nonatomic,strong)UIPageControl *myPageControl; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //初始化scroolview大小為屏幕大小 UIScrollView *rotateScrollView = [[UIScrollView alloc]initWithFrame:self.view.frame]; //設(shè)置滾動范圍 rotateScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*3, CGRectGetHeight(self.view.frame)); //設(shè)置分頁效果 rotateScrollView.pagingEnabled = YES; //水平滾動條隱藏 rotateScrollView.showsHorizontalScrollIndicator = NO; //添加三個子視圖,uilabel類型 for (int i=0; i<3; i++) { UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))]; subLabel.tag = 1000+i; subLabel.text = [NSString stringWithFormat:@"我是第%d個視圖",i]; [subLabel setFont:[UIFont systemFontOfSize:80]]; subLabel.adjustsFontSizeToFitWidth = YES; [subLabel setBackgroundColor:[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0]]; [rotateScrollView addSubview:subLabel]; } UILabel *tempLabel = [rotateScrollView viewWithTag:1000]; //為滾動視圖的右邊添加一個視圖,使得它和第一個視圖一模一樣。 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*3, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))]; label.backgroundColor = tempLabel.backgroundColor; label.text = tempLabel.text; label.font = tempLabel.font; label.adjustsFontSizeToFitWidth = YES; [rotateScrollView addSubview:label]; [self.view addSubview:rotateScrollView]; rotateScrollView.tag = 1000; self.myPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-50, CGRectGetWidth(self.view.frame), 50)]; self.myPageControl.numberOfPages = 3; self.myPageControl.currentPage = 0; [self.view addSubview:self.myPageControl]; //啟動定時器 self.rotateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeView) userInfo:nil repeats:YES]; //為滾動視圖指定代理 rotateScrollView.delegate = self; } #pragma mark -- 滾動視圖的代理方法 //開始拖拽的代理方法,在此方法中暫停定時器。 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ NSLog(@"正在拖拽視圖,所以需要將自動播放暫停掉"); //setFireDate:設(shè)置定時器在什么時間啟動 //[NSDate distantFuture]:將來的某一時刻 [self.rotateTimer setFireDate:[NSDate distantFuture]]; } //視圖靜止時(沒有人在拖拽),開啟定時器,讓自動輪播 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ //視圖靜止之后,過1.5秒在開啟定時器 //[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]] 返回值為從現(xiàn)在時刻開始 再過1.5秒的時刻。 NSLog(@"開啟定時器"); [self.rotateTimer setFireDate:[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]]]; } //定時器的回調(diào)方法 切換界面 - (void)changeView{ //得到scrollView UIScrollView *scrollView = [self.view viewWithTag:1000]; //通過改變contentOffset來切換滾動視圖的子界面 float offset_X = scrollView.contentOffset.x; //每次切換一個屏幕 offset_X += CGRectGetWidth(self.view.frame); //說明要從最右邊的多余視圖開始滾動了,最右邊的多余視圖實際上就是第一個視圖。所以偏移量需要更改為第一個視圖的偏移量。 if (offset_X > CGRectGetWidth(self.view.frame)*3) { scrollView.contentOffset = CGPointMake(0, 0); } //說明正在顯示的就是最右邊的多余視圖,最右邊的多余視圖實際上就是第一個視圖。所以pageControl的小白點需要在第一個視圖的位置。 if (offset_X == CGRectGetWidth(self.view.frame)*3) { self.myPageControl.currentPage = 0; }else{ self.myPageControl.currentPage = offset_X/CGRectGetWidth(self.view.frame); } //得到最終的偏移量 CGPoint resultPoint = CGPointMake(offset_X, 0); //切換視圖時帶動畫效果 //最右邊的多余視圖實際上就是第一個視圖,現(xiàn)在是要從第一個視圖向第二個視圖偏移,所以偏移量為一個屏幕寬度 if (offset_X >CGRectGetWidth(self.view.frame)*3) { self.myPageControl.currentPage = 1; [scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.view.frame), 0) animated:YES]; }else{ [scrollView setContentOffset:resultPoint animated:YES]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS11.3以下modal中input光標(biāo)錯位的解決方法
這篇文章主要介紹了iOS11.3以下modal中input光標(biāo)錯位的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12IOS開發(fā)Swift?與?OC相互調(diào)用詳解
這篇文章主要為大家介紹了IOS開發(fā)Swift?與?OC相互調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08IOS實現(xiàn)左右兩個TableView聯(lián)動效果
在我們?nèi)粘i_發(fā)IOS中,經(jīng)常見到兩個tableview的聯(lián)動,滑動一側(cè)tableview,另一側(cè)tableview跟著滑動,其實實現(xiàn)起來比較簡單,只是需要搞清楚他們之間的區(qū)別和聯(lián)系,下面一起來看看如何實現(xiàn)。2016-08-08iOS的HTTP請求和請求回執(zhí)類用法小結(jié)
這里為大家整理了iOS的HTTP請求和請求回執(zhí)類用法小結(jié),包括發(fā)送請求的NSURLRequest、NSMutableURLRequest和負(fù)責(zé)回復(fù)的NSURLResponse類的常用方法和屬性,需要的朋友可以參考下2016-06-06簡單講解iOS應(yīng)用開發(fā)中的MD5加密的相關(guān)使用
這篇文章主要介紹了iOS應(yīng)用開發(fā)中的MD5加密的相關(guān)使用,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12iOS App設(shè)計模式開發(fā)中對迭代器模式的使用示例
這篇文章主要介紹了iOS App設(shè)計模式開發(fā)中對迭代器模式的使用示例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03