iOS— UIScrollView和 UIPageControl之間的那些事,iosuipagecontrol

來源:互聯網
上載者:User

iOS— UIScrollView和 UIPageControl之間的那些事,iosuipagecontrol

本代碼主要實現在固定的位置滑動圖片可以切換。

目錄圖如下:

 

ViewController.h

#import <UIKit/UIKit.h>// 通過宏定義定義寬和高#define WIDTH self.view.frame.size.width#define HEIGHT self.view.frame.size.height@interface ViewController : UIViewController<UIScrollViewDelegate>@property(strong,nonatomic) UIScrollView *myScrollView;@property(strong,nonatomic) UIPageControl *myPageControl;// 儲存圖片的集合@property(strong,nonatomic)NSMutableArray *imageArray;// 儲存照片@property(strong,nonatomic)UIImageView *firstimage;@property(strong,nonatomic) UIImageView *secondimage;@property(strong,nonatomic) UIImageView *thirimage;// 當前頁碼@property(assign,nonatomic)int currentPage;@end

ViewController.m

1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 self.myScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)]; 12 self.myScrollView.backgroundColor=[UIColor colorWithRed:0.315 green:0.843 blue:0.892 alpha:1.000]; 13 self.myScrollView.contentSize=CGSizeMake(3*WIDTH, HEIGHT); 14 // 設定分頁 15 self.myScrollView.pagingEnabled=YES; 16 // 隱藏捲軸 17 self.myScrollView.showsHorizontalScrollIndicator=NO; 18 // 鎖定滾動方向 19 self.myScrollView.directionalLockEnabled=YES; 20 // 引用代理 21 self.myScrollView.delegate=self; 22 23 [self.view addSubview:self.myScrollView]; 24 25 self.myPageControl=[[UIPageControl alloc] init]; 26 CGSize PageSize=CGSizeMake(120, 40); 27 self.myPageControl.frame=CGRectMake((WIDTH-PageSize.width)/2, HEIGHT-PageSize.height-40, PageSize.width, PageSize.height); 28 self.myPageControl.backgroundColor=[UIColor clearColor]; 29 30 // 設定當前頁 31 self.myPageControl.currentPage=0; 32 // 分頁 33 self.myPageControl.numberOfPages=4; 34 35 [self.view addSubview:self.myPageControl]; 36 37 // 初始化儲存圖片的集合 38 self.imageArray=[NSMutableArray arrayWithCapacity:1]; 39 for (int i=1; i<5; i++) { 40 UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d",i]]; 41 [self.imageArray addObject:image]; 42 } 43 44 self.firstimage =[[UIImageView alloc] init]; 45 self.secondimage=[[UIImageView alloc] init]; 46 self.thirimage =[[UIImageView alloc] init]; 47 // 當前頁碼 48 self.currentPage=0; 49 50 [self reloadImage]; 51 52 } 53 54 -(void)reloadImage 55 { 56 // 第一種情況, 第一頁 57 if (self.currentPage==0) { 58 self.firstimage.image=[self.imageArray lastObject]; 59 self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage]; 60 self.thirimage.image=[self.imageArray objectAtIndex:self.currentPage+1]; 61 62 } 63 64 // 第二種情況,最後一頁 65 else if (self.currentPage==self.imageArray.count-1){ 66 self.firstimage.image=[self.imageArray objectAtIndex:self.currentPage-1]; 67 self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage]; 68 self.thirimage.image=[self.imageArray objectAtIndex:0]; 69 70 } 71 72 // 第三種情況 中間頁 73 else{ 74 self.firstimage.image=[self.imageArray objectAtIndex:self.currentPage-1]; 75 self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage]; 76 self.thirimage.image=[self.imageArray objectAtIndex:self.currentPage+1]; 77 } 78 79 self.firstimage.frame=CGRectMake(0, 0, WIDTH, HEIGHT); 80 self.secondimage.frame=CGRectMake(WIDTH, 0, WIDTH, HEIGHT); 81 self.thirimage.frame=CGRectMake(WIDTH*2, 0, WIDTH, HEIGHT); 82 83 [self.myScrollView addSubview:self.firstimage]; 84 [self.myScrollView addSubview:self.secondimage]; 85 [self.myScrollView addSubview:self.thirimage]; 86 87 self.myScrollView.contentOffset=CGPointMake(WIDTH, 0); 88 } 89 90 #pragma mark scrollView Delegate 91 //在滾動結束狀態轉換 92 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 93 { 94 float x=self.myScrollView.contentOffset.x; 95 96 // 向左 97 if (x<0||x==0) { 98 if (self.currentPage==0) { 99 self.currentPage=(int)self.imageArray.count-1;100 }else{101 self.currentPage--;102 }103 }104 105 // 向右106 if (x>2*WIDTH||x==2*WIDTH) {107 if (self.currentPage==(int)self.imageArray.count-1) {108 self.currentPage=0;109 }else{110 self.currentPage++;111 }112 }113 114 self.myPageControl.currentPage=self.currentPage;115 [self reloadImage];116 }View Code

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.