UIScrollView实现图片浏览

一下内容主要讲何如使用UIScrollView建立三个视图来循环显示多个图像信息,默认多个imageView为三个或者三个以上。

以下是代码部分:

#import "MainViewController.h"


#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface MainViewController () <UIScrollViewDelegate>
{
    
}
@end

@implementation MainViewController
{
    UIScrollView *myScrollView;   //滑动视图
    UIImageView *one;               //第一个图片
    UIImageView *two;               //第二张图片
    UIImageView *thr;               //第三张图片
    NSMutableArray *images;         //储存显示信息的数组
    NSInteger j;                    //显示图片控制数字
    NSInteger max;                  //最大图片个数,表示图片数组的边界
}


- (void)viewDidLoad{
    // 设置初始图片显示控制数字
    j = 6;
    [super viewDidLoad];
    [self _createViews];
    
}


- (void)_createViews{
    myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64)];
    myScrollView.pagingEnabled = YES;
    myScrollView.delegate = self;
    
    // 设置滑动范围 ,注意y值不能设为0否则不会执行
    myScrollView.contentSize = CGSizeMake(3*kScreenWidth, 1);
    myScrollView.showsVerticalScrollIndicator = NO;
    myScrollView.showsHorizontalScrollIndicator = NO;
    myScrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main@2x"]];
    
    images = [[NSMutableArray alloc]init];
    for (int i = 1; i < 7; i++) {
        NSString *name = [NSString stringWithFormat:@"%i",i];
        UIImage *ima = [UIImage imageNamed:name];
        [images addObject:ima];
    }
    
    max = images.count;
    one = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64)];

    
    //设置初始图片信息
    if (j <= 0) {
        [one setImage:images[max-1]];
    }else{
        [one setImage:images[j-1]];
    }
    if (j > max-1) {
        j = max-1;
    }
    one.contentMode = UIViewContentModeScaleAspectFit;
    two = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight-64)];
    [two setImage:images[j]];
    two.contentMode = UIViewContentModeScaleAspectFit;
    if (j == max-1){
        thr = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth*2, 0, kScreenWidth, kScreenHeight-64)];
        [thr setImage:images[0]];
    }else{
        thr = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth*2, 0, kScreenWidth, kScreenHeight-64)];
        [thr setImage:images[j+1]];
    }
 
    thr.contentMode = UIViewContentModeScaleAspectFit;
    
    [myScrollView addSubview:one];
    [myScrollView addSubview:two];
    [myScrollView addSubview:thr];
    [self.view addSubview:myScrollView];
    
    [myScrollView scrollRectToVisible:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight-64) animated:NO];
    
    
    
    
    
    
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    CGFloat width = myScrollView.frame.size.width;
    //当手指滑动scrollview,而scrollview减速停止的时候 开始计算当前的图片的位置
    int currentPage = scrollView.contentOffset.x/width;
    if (currentPage == 0) {
        // 视图左移一个单位
        [myScrollView scrollRectToVisible:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight-64) animated:NO];
        // [one sd_setImageWithURL:imageUrl[_slecte+1]];
        // 对显示信息进行刷新
        j = j - 1;
        if (j == 0) {
            
            [one setImage:images[max-1]];
            [two setImage:images[j]];
            [thr setImage:images[j+1]];
            
        }else if (j < 0) {
            j = max-1;
            [one setImage:images[j-1]];
            [two setImage:images[j]];
            [thr setImage:images[0]];
            
        }
        else{
            [one setImage:images[j-1]];
            [two setImage:images[j]];
            [thr setImage:images[j+1]];
        }
    }
    else if (currentPage == 2) {
        // 视图右移一个单位
        [myScrollView scrollRectToVisible:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight-64) animated:NO];
        // 对显示信息进行刷新
        j = j + 1;
        if (j == max-1) {
            [one setImage:images[j-1]];
            [two setImage:images[j]];
            [thr setImage:images[0]];
        }else if(j == max){
            j = 0;
            [one setImage:images[max-1]];
            [two setImage:images[j]];
            [thr setImage:images[j+1]];
        }else{
            [one setImage:images[j-1]];
            [two setImage:images[j]];
            [thr setImage:images[j+1]];
        }
    }
    
}



@end

实现结果:


    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值