ScrollView 之 實現檢視的迴圈顯示
檢視的迴圈現實一般採用的是412341的方法,先面試例子
#import "ViewController.h"
#define imageWidth 300
#define imageHeight 450
@interface ViewController ()<UIScrollViewDelegate>
{
NSArray * _sourceArray;
UIScrollView * _scrollView;
UIPageControl * _pageControl;
}
@end
@implementation ViewController
-(void)loadView{
[super loadView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
_sourceArray = [NSArray arrayWithObjects:@"A.png",@"B.png",@"C.png",@"E.png", @"F.png",@"G.png",@"H.png",@"I.png",@"J.png",@"K.png",@"L.png",@"M.png",nil];
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 20, imageWidth, imageHeight)];
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.contentSize = CGSizeMake(imageWidth*([_sourceArray count]+2), imageHeight);
[self.view addSubview:_scrollView];
UIImageView * firstImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imageWidth, imageHeight)];
firstImage.image = [UIImage imageNamed:[_sourceArray lastObject]];
[_scrollView addSubview:firstImage];
for (int i = 1; i <= [_sourceArray count]; i++) {
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*imageWidth, 0, imageWidth, imageHeight)];
imageView.image = [UIImage imageNamed:[_sourceArray objectAtIndex:i - 1]];
[_scrollView addSubview:imageView];
}
UIImageView * lastImage = [[UIImageView alloc]initWithFrame:CGRectMake(([_sourceArray count]+1)*imageWidth, 0, imageWidth, imageHeight)];
lastImage.image = [UIImage imageNamed:[_sourceArray objectAtIndex:0]];
[_scrollView addSubview:lastImage];
[_scrollView scrollRectToVisible:CGRectMake(imageWidth, 0, imageWidth, imageHeight) animated:NO];
_pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, _scrollView.frame.origin.y + _scrollView.frame.size.height - 20, imageWidth, 20)];
_pageControl.numberOfPages = _sourceArray.count;
_pageControl.currentPage = 0;
_pageControl.enabled = YES;
[self.view addSubview:_pageControl];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int currentPage = scrollView.contentOffset.x/imageWidth;
if (currentPage == [_sourceArray count]+1) {
[_scrollView scrollRectToVisible:CGRectMake(imageWidth, 0, imageWidth, imageHeight) animated:NO];
_pageControl.currentPage = 0;
}else if (currentPage == 0){
[_scrollView scrollRectToVisible:CGRectMake(imageWidth*([_sourceArray count]), 0, imageWidth, imageHeight) animated:NO];
_pageControl.currentPage = _sourceArray.count - 1;
}else{
_pageControl.currentPage = currentPage - 1;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
上面就是實現的所有程式碼,主要就是用到了scrollview的一個代理方法,和scrollview的一個scrollRectToVisible:方法。
相關文章
- Android之重寫ScrollView實現兩個ScrollView的同步滾動顯示AndroidView
- 字幕迴圈顯示原理
- Android 網路圖片檢視顯示的實現方法Android
- for迴圈—不同div顯示不同樣式
- Render函式進階: 實現資料迴圈, 或者顯示下拉框函式
- android 實現類似qq未讀訊息點選迴圈顯示Android
- vue迴圈顯示base64圖片Vue
- iOS開發:Swift實現的輪播圖、無限迴圈檢視控制元件iOSSwift控制元件
- 淺談迴圈之硬體級實現
- Oracle顯示遊標的使用及遊標for迴圈Oracle
- 價格顯示檢視封裝封裝
- Xamarin.Forms的滾動檢視ScrollViewORMView
- CSS實現限制顯示的字數,超出顯示"..."CSS
- Xamarin iOS教程之檢視顯示影像iOS
- JS優化迴圈之展開迴圈JS優化
- 迴圈佇列C++實現佇列C++
- 迴圈佇列的實現及細節佇列
- 單向迴圈連結串列的實現
- ffmpeg播放器實現詳解 - 視訊顯示播放器
- C# WPF 用MediaElement控制元件實現視訊迴圈播放C#控制元件
- scss中迴圈之@for迴圈佈局畫圓CSS
- JavaScript的map迴圈、forEach迴圈、filter迴圈、reduce迴圈、reduceRight迴圈JavaScriptFilter
- 全域性元件實現遞迴樹,避免迴圈引用元件遞迴
- ArrayList-Linked-Set的迴圈中刪除資料的錯誤顯示
- 佇列的一種實現:迴圈佇列佇列
- YJango的迴圈神經網路——實現LSTMGo神經網路
- CSS實現迴圈無縫滾動CSS
- 使用 call/cc 實現計數迴圈
- for 迴圈境實現遊標LOOP提取OOP
- 11 UML中的邏輯檢視、程序檢視、實現檢視、部署檢視
- Java之 迴圈(三)Java
- Geoserver + MySQL實現圖層顯示和文字顯示ServerMySql
- 11,javase程式碼實戰-迴圈控制——for迴圈實現九九乘法表(二)Java
- V$SQL檢視顯示結果異常的診斷SQL
- Vue mock模擬獲取資料 迴圈遍歷檢視VueMock
- YJango的迴圈神經網路——scan實現LSTMGo神經網路
- Python for迴圈內部實現的一個samplePython
- ExpandableListView實現二級列表的顯示View