3个UIimageView实现图片的循环切换

该博客介绍了如何使用Objective-C在iOS应用中通过UIScrollView实现多个UIImageView的图片循环切换效果。作者通过设置ScrollView的contentSize、pagingEnabled属性,并在滚动过程中调整UIImageView的位置和图片,实现了平滑的轮播功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "ViewController.h"

#define KWidth self.view.frame.size.width

#define KHeight self.view.frame.size.height



@interface ViewController ()<UIScrollViewDelegate>


@property(nonatomic,weak) UIScrollView *scroll;

/**存放图片名的数组*/

@property(nonatomic,strong) NSMutableArray *imageNameArray;

/**存放3UIimageView的数组*/

@property(nonatomic,strong) NSMutableArray *imageViewArray;

/**存放3UIimageView中未被利用的UIimageView的数组*/

@property(nonatomic,strong) NSMutableArray *imageUnusedViewArray;


@end


@implementation ViewController


//懒加载(用到时才加载)

-(NSMutableArray *)imageNameArray{

    if (_imageNameArray==nil)

    {

        _imageNameArray = [NSMutableArray array];

    }

    return _imageNameArray;

}


-(NSMutableArray *)imageViewArray{

    if (_imageViewArray==nil)

    {

        _imageViewArray = [NSMutableArray array];

    }

    return _imageViewArray;

}


-(NSMutableArray *)imageUnusedViewArray{

    if (_imageUnusedViewArray==nil)

    {

        _imageUnusedViewArray = [NSMutableArray array];

    }

    return _imageUnusedViewArray;


}




- (void)viewDidLoad {

    [super viewDidLoad];


    

    //加载数据

    [self _loadData];

    

    //加载视图

    [self _loadView];


    

}


#pragma mark - 加载数据

-(void)_loadData{



    for (int i=1; i<12; i++)

    {

        NSString *imageName = [NSString stringWithFormat:@"t%i.jpg",i];

        [self.imageNameArray addObject:imageName];

        

    }


}


#pragma  mark - 加载视图

-(void)_loadView{


    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:scroll];

    scroll.contentSize = CGSizeMake(KWidth*self.imageNameArray.count, 0);

    scroll.pagingEnabled = YES;

    scroll.delegate = self;

    self.scroll = scroll;

    

    //添加三个视图

    for (int i=0; i<3; i++)

    {

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*KWidth, 0, KWidth, KHeight)];

        

        imageView.image = [UIImage imageNamed:self.imageNameArray[i]];

        

        [self.scroll addSubview:imageView];

        [self.imageViewArray addObject:imageView];



    }

}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{


    //只有contentOffset是整数时才进行下面判断,这样做就相当于-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView方法

    if (scrollView.contentOffset.x/KWidth==(int)(scrollView.contentOffset.x/KWidth))

    {

        //获取未利用的imageView

        [self getUnusedView];

        

        //不是在第一页,也不是在最后一页的时候

        if (scrollView.contentOffset.x>0&&scrollView.contentOffset.x<KWidth*(self.imageNameArray.count-1))

        {

            [self setImageFrameWithImage:self.imageUnusedViewArray[0] andImageWith:frontView];

            

            [self setImageFrameWithImage:self.imageUnusedViewArray[1] andImageWith:nextView];

            

            //如果当没利用的imageView数量大于3时,证明当前页面也没有利用,所以当前页面就没有图片(当滑的太快时就会出现以上情况)

            if (self.imageUnusedViewArray.count>2)

            {

                [self setImageFrameWithImage:self.imageUnusedViewArray[2] andImageWith:defineView];

            }

            

            

        }

        //如果实在第一页

        else if(scrollView.contentOffset.x==0)

        {

            [self setImageFrameWithImage:self.imageViewArray[0] andImageWith:defineView];

            [self setImageFrameWithImage:self.imageViewArray[1] andImageWith:nextView];

        }

        //如果是在最后一页

        else

        {

            [self setImageFrameWithImage:self.imageViewArray[0] andImageWith:defineView];

            

            [self setImageFrameWithImage:self.imageViewArray[1] andImageWith:frontView];

            

        }

    }

    //如果contentOffset不是整数就返回

    else

    {

    

        return;

    

    }




}



#pragma mark - 设置frame和图片

-(void)setImageFrameWithImage:(UIImageView *)imageView andImageWith:(NSInteger)flag{


    //设置frame

    CGFloat space = flag*KWidth;

    CGFloat X = self.scroll.contentOffset.x+space;

    imageView.frame = CGRectMake(X, 0, KWidth, KHeight);

    

    

    //设置图片

    NSInteger imageIndex = X/KWidth;

    imageView.image =  [UIImage imageNamed:self.imageNameArray[imageIndex]];



}


#pragma mark - 获得未被利用的imageView

-(void)getUnusedView{


    for (int i=0; i<self.imageViewArray.count; i++)

    {

        UIImageView *imageV = self.imageViewArray[i];


        if (imageV.frame.origin.x!=self.scroll.contentOffset.x)

        {

            [self.imageUnusedViewArray addObject:imageV];

        }

    }


}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值