首页 > 代码库 > iOS开发之实现图片自动切换(类似android画廊效果)

iOS开发之实现图片自动切换(类似android画廊效果)

#import "ViewController.h"
#define ImageViewCount 5

@interface ViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *imageScrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *imageViewPageControl;
@property (strong, nonatomic) NSTimer *timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self addImageView2ScrollView];
    self.imageScrollView.contentSize = CGSizeMake(self.imageScrollView.frame.size.width * ImageViewCount, 0);

    self.imageScrollView.delegate = self;
    self.imageScrollView.pagingEnabled = YES;//UIScrollView支持拖动分页
    self.imageViewPageControl.numberOfPages  = ImageViewCount;
    
    [self addScrollTimer];
}

- (void)rotatePic{
    int currentPageIndex = self.imageViewPageControl.currentPage;
    if(++currentPageIndex == 5){
        currentPageIndex = 0;
    }
    CGFloat offsetX = currentPageIndex * self.imageScrollView.frame.size.width;
    [UIView animateWithDuration:1 animations:^{
        self.imageScrollView.contentOffset = CGPointMake(offsetX, 0);
    }];
}

/**添加图片到imageScrollView*/
- (void)addImageView2ScrollView{
    CGFloat imageWidth = self.imageScrollView.frame.size.width;
    CGFloat imageHeight = self.imageScrollView.frame.size.height;
    for(int i = 0;i <= ImageViewCount;i++){
        UIImageView *imageInScroll = [[UIImageView alloc] init];
        imageInScroll.frame = CGRectMake(i * imageWidth, 0, imageWidth, imageHeight);
        imageInScroll.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_%02d",i + 1]];
        [self.imageScrollView addSubview:imageInScroll];
    }
}

// 正滚动时执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offX = self.imageScrollView.contentOffset.x;//(0,0)距离content内部左上顶点的x轴长度
    NSLog(@"~~~~~~~%f ^^^^^^%f", offX, self.imageScrollView.frame.size.width);
    int currentPageIndex = (offX + .5f * self.imageScrollView.frame.size.width) / self.imageScrollView.frame.size.width;
    self.imageViewPageControl.currentPage = currentPageIndex;
}

- (void)addScrollTimer{
    self.timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(rotatePic) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)removeScrollTimer{
    [self.timer invalidate];//释放定时器
    self.timer = nil;
}

// 开始准备滚动时执行 移除定时滚动操作
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"~~~scrollViewWillBeginDragging");
    [self removeScrollTimer];
}

// 结束滚动后执行 添加定时滚动操作
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"~~~scrollViewDidEndDragging");
    [self addScrollTimer];
}
@end

对UIScrollView的运用,以上代码中有详细注释,需注意2点:

1.注意设置contentSize属性。其中contentSize表示scroll内容尺寸大小

2.注意设置代理UIScrollViewDelegate,才可调用其中的方法

对于定时器NSTimer的运用需注意

1.在线程的loop中添加定时器

2.注意使用完成回收NSTimer

iOS开发之实现图片自动切换(类似android画廊效果)