UIScrollView循環滾動(三張圖片循環滾動)

參考了這個鏈接裏的內容:http://www.cocoachina.com/ios/20150803/12879.html


@interface ViewController ()<UIScrollViewDelegate>


@property (strong,nonatomic)UIScrollView *scrollView;

@property (assign,nonatomic)CGSize size;


@property (strong,nonatomic)UIImageView *imageViewLef;

@property (strong,nonatomic)UIImageView *imageViewCenter;

@property (strong,nonatomic)UIImageView *imagViewRight;


@property (strong,nonatomic)NSMutableArray *arrayM;


@property (assign,nonatomic)CGFloat beginX;


@property (assign,nonatomic)NSInteger page;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    self.arrayM = [NSMutableArrayarray];

    

    self.page =0;

    

    [self setupPic];

    

    [selfsetupScrollView];

}


/**

 *  設置scrollView

 */

-(void)setupScrollView

{

    CGRect rect = [UIScreenmainScreen].bounds;

    

    self.size = rect.size;

    

    self.scrollView = [[UIScrollViewalloc]initWithFrame:rect];

    

    [self.scrollViewsetContentSize:CGSizeMake(self.size.width *3 , self.size.height)];

    

    [self.scrollViewsetShowsHorizontalScrollIndicator:NO];

    

    [self.scrollViewsetPagingEnabled:YES];

    

    [self.scrollViewsetDelegate:self];

    

    [self.scrollViewsetContentOffset:CGPointMake(self.size.width,0)];

    

    [self.viewaddSubview:self.scrollView];

    

    [selfsetupImageView];

    

    [selfreloadImageView];

}


/**

 *  設置scrollView中的三個imageView

 */

-(void)setupImageView

{

    self.imageViewLef = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, self.size.width,self.size.height)];

    [self.imageViewLefsetContentMode:UIViewContentModeScaleAspectFill];

    [self.scrollViewaddSubview:self.imageViewLef];

    

    self.imageViewCenter = [[UIImageViewalloc]initWithFrame:CGRectMake(self.size.width,0, self.size.width,self.size.height)];

    [self.imageViewLefsetContentMode:UIViewContentModeScaleAspectFill];

    [self.scrollViewaddSubview:self.imageViewCenter];

    

    self.imagViewRight = [[UIImageViewalloc]initWithFrame:CGRectMake(self.size.width * 2, 0, self.size.width,self.size.height)];

    [self.imageViewLefsetContentMode:UIViewContentModeScaleAspectFill];

    [self.scrollViewaddSubview:self.imagViewRight];

    

    [self.imageViewLefsetBackgroundColor:[UIColorredColor]];

    [self.imageViewCentersetBackgroundColor:[UIColorblueColor]];

    [self.imagViewRightsetBackgroundColor:[UIColorgrayColor]];

}


/**

 *  設置圖片

 */

-(void)setupPic

{

    for (NSInteger i =1; i <= 4; i++) {

        UIImage *image = [UIImageimageNamed:[NSStringstringWithFormat:@"new_feature_%ld",i]];


        [self.arrayMaddObject:image];

    }

}


/**

 *  重新加載圖片

 */

-(void)reloadImageView

{

    if (self.page >=4 ) {

        self.page =0;

    }

    

    if (self.page <0) {

        self.page =3;

    }

    

    NSInteger pre = self.page - 1;

    NSInteger now = self.page;

    NSInteger next = self.page + 1;

    

    if (pre < 0) {

        pre = 3;

    }

    

    if (next >= 4) {

        next = 0;

    }

    

    [self.imageViewLefsetImage:self.arrayM[pre]];

    [self.imageViewCentersetImage:self.arrayM[now]];

    [self.imagViewRightsetImage:self.arrayM[next]];

    

    [self.scrollViewsetContentOffset:CGPointMake(self.size.width,0)];

    

    self.beginX =self.size.width;

}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    [self operation:scrollView];

}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    NSLog(@"x %f",scrollView.contentOffset.x);

    NSLog(@"beginX %f",self.beginX);

    

    /*

     * 當快速拖動時,邊上會出現白邊,然後再有圖片,

     * 當快速左右拖動時,x會大於或者小於beginX,如果是正常拖動x是等於beginX

     */

    

    CGFloat x = scrollView.contentOffset.x;

    

    if (x > self.beginX || x <self.beginX) {

        [self operation:scrollView];

    }

}


/**

 *  前後拖動時的處理

 */

-(void)operation:(UIScrollView *)scrollView

{

    CGFloat x = scrollView.contentOffset.x;

    

    if (x > self.beginX) {

        NSLog(@"");

        

        self.page++;

    }

    

    if (x < self.beginX) {

        NSLog(@"");

        

        self.page--;

    }

    

    self.beginX = x;

    

    [selfreloadImageView];

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章