iOS使用UIScrollView實(shí)現(xiàn)無(wú)限循環(huán)輪播圖效果
本文實(shí)例為大家分享了iOS使用UIScrollView實(shí)現(xiàn)無(wú)限循環(huán)輪播圖的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
// // ViewController.m // 無(wú)限輪播 // // Created by limin on 17/8/23. // Copyright © 2017年 none. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate> /* 定時(shí)器 */ @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è)置滾動(dòng)范圍 rotateScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*3, CGRectGetHeight(self.view.frame)); //設(shè)置分頁(yè)效果 rotateScrollView.pagingEnabled = YES; //水平滾動(dòng)條隱藏 rotateScrollView.showsHorizontalScrollIndicator = NO; //添加三個(gè)子視圖,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個(gè)視圖",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]; //為滾動(dòng)視圖的右邊添加一個(gè)視圖,使得它和第一個(gè)視圖一模一樣。 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]; //啟動(dòng)定時(shí)器 self.rotateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeView) userInfo:nil repeats:YES]; //為滾動(dòng)視圖指定代理 rotateScrollView.delegate = self; } #pragma mark -- 滾動(dòng)視圖的代理方法 //開(kāi)始拖拽的代理方法,在此方法中暫停定時(shí)器。 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ NSLog(@"正在拖拽視圖,所以需要將自動(dòng)播放暫停掉"); //setFireDate:設(shè)置定時(shí)器在什么時(shí)間啟動(dòng) //[NSDate distantFuture]:將來(lái)的某一時(shí)刻 [self.rotateTimer setFireDate:[NSDate distantFuture]]; } //視圖靜止時(shí)(沒(méi)有人在拖拽),開(kāi)啟定時(shí)器,讓自動(dòng)輪播 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ //視圖靜止之后,過(guò)1.5秒在開(kāi)啟定時(shí)器 //[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]] 返回值為從現(xiàn)在時(shí)刻開(kāi)始 再過(guò)1.5秒的時(shí)刻。 NSLog(@"開(kāi)啟定時(shí)器"); [self.rotateTimer setFireDate:[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]]]; } //定時(shí)器的回調(diào)方法 切換界面 - (void)changeView{ //得到scrollView UIScrollView *scrollView = [self.view viewWithTag:1000]; //通過(guò)改變contentOffset來(lái)切換滾動(dòng)視圖的子界面 float offset_X = scrollView.contentOffset.x; //每次切換一個(gè)屏幕 offset_X += CGRectGetWidth(self.view.frame); //說(shuō)明要從最右邊的多余視圖開(kāi)始滾動(dòng)了,最右邊的多余視圖實(shí)際上就是第一個(gè)視圖。所以偏移量需要更改為第一個(gè)視圖的偏移量。 if (offset_X > CGRectGetWidth(self.view.frame)*3) { scrollView.contentOffset = CGPointMake(0, 0); } //說(shuō)明正在顯示的就是最右邊的多余視圖,最右邊的多余視圖實(shí)際上就是第一個(gè)視圖。所以pageControl的小白點(diǎn)需要在第一個(gè)視圖的位置。 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); //切換視圖時(shí)帶動(dòng)畫效果 //最右邊的多余視圖實(shí)際上就是第一個(gè)視圖,現(xiàn)在是要從第一個(gè)視圖向第二個(gè)視圖偏移,所以偏移量為一個(gè)屏幕寬度 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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS11.3以下modal中input光標(biāo)錯(cuò)位的解決方法
這篇文章主要介紹了iOS11.3以下modal中input光標(biāo)錯(cuò)位的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12IOS開(kāi)發(fā)Swift?與?OC相互調(diào)用詳解
這篇文章主要為大家介紹了IOS開(kāi)發(fā)Swift?與?OC相互調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08IOS實(shí)現(xiàn)左右兩個(gè)TableView聯(lián)動(dòng)效果
在我們?nèi)粘i_(kāi)發(fā)IOS中,經(jīng)常見(jiàn)到兩個(gè)tableview的聯(lián)動(dòng),滑動(dòng)一側(cè)tableview,另一側(cè)tableview跟著滑動(dòng),其實(shí)實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單,只是需要搞清楚他們之間的區(qū)別和聯(lián)系,下面一起來(lái)看看如何實(shí)現(xiàn)。2016-08-08iOS拍照后圖片自動(dòng)旋轉(zhuǎn)90度的完美解決方法
今天開(kāi)發(fā)一個(gè)拍照獲取照片的功能的時(shí)候, 發(fā)現(xiàn)上傳之后圖片會(huì)自動(dòng)旋轉(zhuǎn)90.在測(cè)試中發(fā)現(xiàn)只要是圖片大于2M, 系統(tǒng)就會(huì)自動(dòng)翻轉(zhuǎn)照片。下面小編通過(guò)本文給大家分享下解決辦法2016-12-12iOS的HTTP請(qǐng)求和請(qǐng)求回執(zhí)類用法小結(jié)
這里為大家整理了iOS的HTTP請(qǐng)求和請(qǐng)求回執(zhí)類用法小結(jié),包括發(fā)送請(qǐng)求的NSURLRequest、NSMutableURLRequest和負(fù)責(zé)回復(fù)的NSURLResponse類的常用方法和屬性,需要的朋友可以參考下2016-06-06簡(jiǎn)單講解iOS應(yīng)用開(kāi)發(fā)中的MD5加密的相關(guān)使用
這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中的MD5加密的相關(guān)使用,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12iOS App設(shè)計(jì)模式開(kāi)發(fā)中對(duì)迭代器模式的使用示例
這篇文章主要介紹了iOS App設(shè)計(jì)模式開(kāi)發(fā)中對(duì)迭代器模式的使用示例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03