iOS - 功能模块 - 幻灯片切换

时间:2022-11-03 20:31:04

依据性能需求,实现iOS幻灯片切换功能。

1.图片少的情况下

iOS - 功能模块 - 幻灯片切换
Demo:https://github.com/shileseal/SLCarouselDemo


#import "ViewController.h"

@interfaceViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutletUIScrollView *scrollView;
@property (weak, nonatomic) IBOutletUIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [superviewDidLoad];

    CGFloat imageW = self.scrollView.frame.size.width;
    CGFloat imageH = self.scrollView.frame.size.height;
    CGFloat imageY = 0;
    NSInteger number = 5;

    for (int i = 0; i < 5; i++) {

        UIImageView *imageView = [[UIImageViewalloc]init];
        NSString *imageUrl = [NSStringstringWithFormat:@"%d",i];
        NSLog(@"%@",imageUrl);

        imageView.image        = [UIImageimageNamed:imageUrl];

        CGFloat imageX         = i * imageW;
        imageView.frame        = CGRectMake(imageX, imageY, imageW, imageH);

        self.scrollView.showsHorizontalScrollIndicator = NO;

        [self.scrollViewaddSubview:imageView];

    }
    CGFloat contentW = number * imageW;
    self.scrollView.contentSize = CGSizeMake(contentW, 0);

    self.scrollView.pagingEnabled = YES;

    self.scrollView.delegate = self;

    [selfaddTimer];
}

- (void)nextImage {

    int page = (int)self.pageControl.currentPage;
    if (page == 4){
        page = 0;
    } else {

        page ++;
    }

    CGFloat x = page * self.scrollView.frame.size.width;
    self.scrollView.contentOffset = CGPointMake(x, 0);
}

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

    NSLog(@"滚动中");
    CGFloat scrollviewW = scrollView.frame.size.width;
    CGFloat x = self.scrollView.contentOffset.x;
    int page = (x + scrollviewW/2)/scrollviewW;
    self.pageControl.currentPage = page;
}

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

    [selfremoveTimer];
}

- (void)addTimer {
    self.timer = [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(nextImage) userInfo:nilrepeats:YES];
}

- (void)removeTimer {
    [self.timerinvalidate];

}

@end

2.图片多的情况下,使用UICollectionView进行复用

iOS - 功能模块 - 幻灯片切换
Demo:https://github.com/shileseal/SLScrollViewReuseDemo

#import "ViewController.h"

@interfaceViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutletUICollectionView *collectionView;

@end

    staticNSString *identifier = @"cell";
@implementation ViewController

- (void)viewDidLoad {
    [superviewDidLoad];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;


    [self.collectionViewregisterClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:identifier];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return50;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierforIndexPath:indexPath];

    NSString *imageUrl = [NSStringstringWithFormat:@"%ld.png",(long)indexPath.row];
    NSLog(@"%@",imageUrl);
    UIImageView *imageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:imageUrl]];

    #warning 为什么这么写就无法显示呢?
// UIImageView *imageView = [[UIImageView alloc]init];
// imageView.image = [UIImage imageNamed:imageUrl];


    [cell addSubview:imageView];
    [cell bringSubviewToFront:imageView];

    switch (indexPath.row%2) {
        case1:
            cell.backgroundColor = [UIColorwhiteColor];
            break;

        default:
            cell.backgroundColor = [UIColorblueColor];
            break;
    }
    NSLog(@"%@",cell);

    return cell;
}

@end

3.iCarousel

https://github.com/nicklockwood/iCarousel