網站首頁 編程語言 正文
本文實例為大家分享了iOS簡單實現輪播圖效果的具體代碼,供大家參考,具體內容如下
平常在開發過程中,首頁的輪播圖總是少不了,輪播圖我們都知道肯定是要使用 UIScrollView ,難點就在最后一張圖片被滑動時,如何回到第一張圖片以及第一張滑動到最后一張。
我們可以使用如下方式實現輪播圖,在劃到3后面的1后,設置 contentOffset 回到最先的1,并設置 pageControl ,即可達到效果 (從1劃到3也同理)
看一下效果:
完成這種輪播圖,我們的 View 需要如下的屬性和方法
@interface RoundView : UIView
?
@property (nonatomic,strong) UIScrollView ? *scrollView;
//存放ScrollView每一個page的圖片的array
@property (nonatomic) NSMutableArray ? ? ? ?*imgArray;
@property (nonatomic) UIPageControl ? ? ? ? *pageControl;
//定時器
@property (nonatomic,strong) NSTimer ? ? ? ?*__nullable timer;
?
- (instancetype)initWithFrame:(CGRect)frame imgArray:(NSArray *)array;
?
@end
在創建View的時候使用 initWithFrame:(CGRect)frame imgArray:(NSArray *)array,傳入需要展示的 imgArray ,在 view 內進行處理。
在該方法的實現中,首先就是對圖片數組array的處理,得到我們需要的imgArray
- (instancetype)initWithFrame:(CGRect)frame imgArray:(NSArray *)array{
? ? self=[super initWithFrame:frame];
? ??
? ? self.imgArray=[[NSMutableArray alloc]init];
? ? [self.imgArray addObject:[array lastObject]];
? ? [self.imgArray addObjectsFromArray:array];
? ? [self.imgArray addObject:[array firstObject]];
}
這樣,我們的 imgArray 就變成了最開始演示原理的樣式,為 @[first object,array,lastobject]
然后我們初始化一下 ScrollView
self.scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
? ? [self.scrollView.layer setCornerRadius:10.0];
? ? self.scrollView.contentSize=CGSizeMake(frame.size.width * self.imgArray.count, frame.size.height);
? ? self.scrollView.pagingEnabled=YES;
? ? self.scrollView.contentOffset=CGPointMake(frame.size.width, 0);
? ? self.scrollView.showsVerticalScrollIndicator=NO;
? ? self.scrollView.showsHorizontalScrollIndicator=NO;
? ? self.scrollView.delegate=self;
? ? [self.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
很簡單,就是一些屬性、代理的設置 根據 imgArray 的大小設置 contentSize ,另外對 contentOffset 添加一個觀察者,方便后續對pageControl和contentOffset進行設置。
然后,我們根據 imgArray 將圖片填入到 scrollView 中
for(int i=0;i<self.imgArray.count;i++){
? ? ? ? CGRect imgFrame=CGRectMake(frame.size.width* i, 0, frame.size.width , frame.size.height);
? ? ? ? UIImageView* imgView=[[UIImageView alloc]initWithFrame:imgFrame];
? ? ? ? //這里我就不傳入圖片了,測試的imgArray中為顏色,使用顏色替代
? ? ? ? imgView.backgroundColor=self.imgArray[i];
? ? ? ? [self.scrollView addSubview:imgView];
? ? }
然后對pageControl和timer進行初始化
self.pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, frame.size.height-20, frame.size.width, 20)];
? ? self.pageControl.numberOfPages=array.count;
? ? self.pageControl.currentPage=0;
? ? self.pageControl.tintColor=[UIColor whiteColor];
? ??
? ? self.timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
? ? //添加到runloop中
? ? NSRunLoop *runloop=[NSRunLoop currentRunLoop];
? ? [runloop addTimer:self.timer forMode:NSRunLoopCommonModes];
? ??
? ? [self addSubview:self.scrollView];
? ? [self addSubview:self.pageControl];
這里設置的是5秒鐘后自動滾動輪播圖,可以根據需要自己設置,scrollmage 方法后面會講
我們看一下我們需要的 scrollView 的代理方法
1.scrollViewDidScroll:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
? ? CGFloat offsetX=scrollView.contentOffset.x;
? ? offsetX+=scrollView.frame.size.width*0.5;
? ??
? ? //因為最前面還有一個imgView
? ? int page=offsetX/scrollView.frame.size.width-1;
? ? self.pageControl.currentPage=page;
? ??
}
此方法用來計算 contentOffset 對應的 pageControl 的 page ,需要注意的是在第一張圖片之前還有最后一張圖片,所以計算的時候需要-1
2.scrollViewWillBgeinDragging:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
? ? [self.timer invalidate];
? ? self.timer=nil;
}
在scrollView即將被劃動時,取消自動輪播,將timer設置為nil
3.scorllViewDidEndDragging: willDecelearate:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
? ? self.timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
? ??
? ? //優先級 設置到當前的runloop中
? ? NSRunLoop *runloop=[NSRunLoop currentRunLoop];
? ? [runloop addTimer:self.timer forMode:NSRunLoopCommonModes];
}
在將要結束劃動的時候,重新設置timer 并添加到當前的runloop中
實現輪播圖的核心代碼
對 contentOffset 的監聽
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
? ??
? ? if([keyPath isEqualToString:@"contentOffset"]){
? ? ? ? //[change objectForKey:NSKeyValueChangeNewKey] 是一個對象
? ? ? ? CGPoint offset= [[change objectForKey:NSKeyValueChangeNewKey] CGPointValue];
? ? ? ? //當劃到3后面的1時
? ? ? ? if(offset.x >= self.scrollView.contentSize.width-self.scrollView.frame.size.width){
? ? ? ? ? ? [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0)];
? ? ? ? ? ? self.pageControl.currentPage=0;
? ? ? ? }
? ? ? ? //當劃到1前面的3時
? ? ? ? else if(offset.x <= 0){
? ? ? ? ? ? [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentSize.width-2*self.scrollView.frame.size.width, 0)];
? ? ? ? ? ? self.pageControl.currentPage=self.pageControl.numberOfPages-1;
? ? ? ? }
? ? }
? ??
}
首先[change objectForKey:NSKeyValueChangeNewKey] 是一個對象,我們可以通過 ?CGPointValue 去得到contentOffset
然后我們對 contentOffset 的 x 進行判斷,如果在最后一張圖片,即3后面的1時,將scrollView的contentOffset 設置為初始的1所在的位置,這里不能設置動畫
同理,如果在1前面的3時,將scrollView的contentOffset 設置為初始的3所在的位置,這里不能設置動畫
需要注意 pageControl 的設置也應該實時設置
最后,我們將自己輪播的方法 scrollImage 填寫
-(void)scrollImage{
? ? ? ? ? ??
? ? NSInteger page=[self.pageControl currentPage];
? ? page++;
? ? CGFloat offsetX= page * self.scrollView.frame.size.width+self.scrollView.frame.size.width;
? ??
? ? [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}
從當前的 page 滾動至下一個 page ,設置動畫
原文鏈接:https://blog.csdn.net/XXJcanbethebest/article/details/122372698
相關推薦
- 2022-08-17 python可視化分析繪制帶趨勢線的散點圖和邊緣直方圖_python
- 2022-02-28 CommonsChunkPlugin 插件使用方法 、 出現報錯 : Error: webpa
- 2022-05-20 SpringBoot整合Mybatis演示
- 2022-10-06 Python中requests庫的基本概念與具體使用方法_python
- 2022-04-11 Xamarin.Forms在安卓機上進行本機調試_實用技巧
- 2022-01-19 webpack5 配置熱更新失效問題
- 2022-08-16 python切片操作方法的實例總結_python
- 2022-04-27 C語言模擬實現密碼輸入的示例代碼_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支