UIscrollView和UIPageControl的迴圈滾動,uipagecontrol迴圈

來源:互聯網
上載者:User

UIscrollView和UIPageControl的迴圈滾動,uipagecontrol迴圈

 

因為昨天在網上找了很久,很多隻能實現向右滾動,而且一張圖一個imageview ,感覺工作量很可怕啊 ,  下面的例子就是不論你多少圖 , 只和我代碼裡面的幾個數值有關,  只需要修改分頁和迴圈i的最大值,當然為了方便 , 您最好把圖片的名字改成有序的 。 方便您添加到可變集合中。

 

 

 

 

 

 

 

 

 

如果這樣的頁面你有五頁 或則更多都可以實現好像從最後一張圖跳到第一張圖。

這個其實總共只有3個image,圖都是用迴圈加進去的 。

上代碼

#import <UIKit/UIKit.h>#define WIDTH self.view.bounds.size.width#define HEIGHT self.view.bounds.size.height@interface ViewController : UIViewController<UIScrollViewDelegate>@property(strong,nonatomic)  UIScrollView *scrollview;@property(strong,nonatomic)  UIPageControl *pagecontrol;//儲存圖片@property(strong,nonatomic)  NSMutableArray * imageArray;//當前頁碼@property(assign,nonatomic)  int  currentPage;//儲存圖片@property(strong,nonatomic) UIImageView * firstImage;@property(strong,nonatomic)  UIImageView * secondImage;@property(strong,nonatomic)  UIImageView *thirdImage;@end

 

 

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, WIDTH,HEIGHT )];        self.scrollview.contentSize=CGSizeMake(WIDTH*3, 0);    //是否分頁    self.scrollview.pagingEnabled=YES;    //添加代理    self.scrollview.delegate=self;    //隱藏捲軸    self.scrollview.showsHorizontalScrollIndicator=NO;    [self.view addSubview:self.scrollview];        self.pagecontrol=[[UIPageControl alloc]initWithFrame:CGRectMake(WIDTH/5*3, HEIGHT/5*4, WIDTH/3, 40)];    //設定當前頁    self.pagecontrol.currentPage=0;    //分頁    self.pagecontrol.numberOfPages=5;    //指定頁碼顏色    self.pagecontrol.currentPageIndicatorTintColor=[UIColor redColor];    self.pagecontrol.pageIndicatorTintColor=[UIColor blueColor];    [self.view addSubview:self.pagecontrol];        //初始化儲存圖片的集合    self.imageArray=[NSMutableArray array];    for (int i=1; i<6; i++) {        UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];        [self.imageArray addObject:image];    }        self.firstImage=[[UIImageView alloc]init];    self.secondImage=[[UIImageView alloc]init];    self.thirdImage=[[UIImageView alloc]init];    //當前頁碼    self.currentPage=0;    [self reloadImage];                    }-(void)reloadImage{    //第一種情況 , 第一頁    if (self.currentPage==0) {        self.firstImage.image=[self.imageArray lastObject];        self.secondImage.image = [self.imageArray objectAtIndex:self.currentPage];        self.thirdImage.image = [self.imageArray objectAtIndex:self.currentPage + 1];    }    //    第二種情況 最後一頁    else if (self.currentPage == self.imageArray.count - 1) {        self.firstImage.image = [self.imageArray objectAtIndex:self.currentPage - 1];        self.secondImage.image = [self.imageArray objectAtIndex:self.currentPage];        self.thirdImage.image = [self.imageArray objectAtIndex:0];    }    //    第三種情況  中間頁    else {        self.firstImage.image = [self.imageArray objectAtIndex:self.currentPage - 1];        self.secondImage.image = [self.imageArray objectAtIndex:self.currentPage];        self.thirdImage.image = [self.imageArray objectAtIndex:self.currentPage + 1];    }    self.firstImage.frame = CGRectMake(0, 0, WIDTH, HEIGHT);    self.secondImage.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);    self.thirdImage.frame = CGRectMake(WIDTH* 2, 0, WIDTH, HEIGHT);        [self.scrollview addSubview:self.firstImage];    [self.scrollview addSubview:self.secondImage];    [self.scrollview addSubview:self.thirdImage];        self.scrollview.contentOffset = CGPointMake(WIDTH, 0);        }-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{        float x= self.scrollview.contentOffset.x;    //向左    if (x<0||x==0) {        if (self.currentPage==0) {            self.currentPage=(int)self.imageArray.count-1;        }        else{            self.currentPage--;        }    }    //向右    if (x > WIDTH * 2 || x == WIDTH * 2) {        if (self.currentPage == (int)self.imageArray.count - 1) {            self.currentPage = 0;        }        else {            self.currentPage++ ;        }    }        self.pagecontrol.currentPage = self.currentPage;        [self reloadImage];        }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.