3個(gè)imageView實(shí)現(xiàn)輪播圖

擼了一個(gè)輪播圖,已上傳github(今天github提交好慢,微軟你干了什么。。。) → 傳送門
使用方法:

1、導(dǎo)入頭文件 #import "YCarouselView.h"

2、

NSArray *a = @[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg"];
YCarouselView *c = [[YCarouselView alloc] initWithFrame:CGRectMake(0, 100, kScreenWidth, 200) localImages:a];
[self.view addSubview:c];

功能:

1、支持定時(shí)輪播
2、支持手動(dòng)滑動(dòng)
3、支持點(diǎn)擊圖片鏈接

重點(diǎn):

1、給UIImageView賦值

// 賦值
- (void)setupCurrentValue{

    NSInteger tc = self.imageArray.count;

    if (self.isUrlImage) {
        [self.middleImgV sd_setImageWithURL:[NSURL URLWithString:self.imageArray[_curIndex]] placeholderImage:nil options:SDWebImageRetryFailed];
        [self.leftImgV sd_setImageWithURL:[NSURL URLWithString:self.imageArray[(tc - 1 + _curIndex)%tc]] placeholderImage:nil options:SDWebImageRetryFailed];
        [self.rightImgV sd_setImageWithURL:[NSURL URLWithString:self.imageArray[(tc + 1 + _curIndex)%tc]] placeholderImage:nil options:SDWebImageRetryFailed];

    } else {
        // 看圖片放在哪,一般都是Assets.xcassets
        self.middleImgV.image = [UIImage imageNamed:self.imageArray[_curIndex]];
        self.leftImgV.image = [UIImage imageNamed:self.imageArray[(tc - 1 + _curIndex)%tc]];
        self.rightImgV.image = [UIImage imageNamed:self.imageArray[(tc + 1 + _curIndex)%tc]];
    }

}

2、計(jì)算當(dāng)前index及設(shè)置scrollView的contentOffset

// 算法邏輯
- (void)slidingAlgorithmWithRight:(BOOL)right animation:(BOOL)animation{
    if (right) {
        if (self.curIndex >= self.imageArray.count - 1) {
            self.curIndex = 0;
        } else {
            self.curIndex ++;
        }

        /*
         設(shè)置setContentOffset:animated:/contentOffset不觸發(fā)scrollViewDidEndDecelerating代理
         */
        if (animation) {
            [UIView animateWithDuration:AnimateDuration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
                self.scrollView.contentOffset = CGPointMake(kScreenWidth*2, 0);
            } completion:^(BOOL finished) {
                [self setupCurrentValue];
                self.scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
            }];
        } else {
            self.scrollView.contentOffset = CGPointMake(kScreenWidth*2, 0);
            [self setupCurrentValue];
            self.scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
        }

    } else{

        if (self.curIndex <= 0) {
            self.curIndex = self.imageArray.count - 1;
        } else {
            self.curIndex --;
        }

        if (animation) {
            [UIView animateWithDuration:AnimateDuration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
                self.scrollView.contentOffset = CGPointMake(0, 0);
            } completion:^(BOOL finished) {
                [self setupCurrentValue];
                self.scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
            }];
        } else {
            self.scrollView.contentOffset = CGPointMake(0, 0);
            [self setupCurrentValue];
            self.scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
        }

    }
    self.pageControl.currentPage = _curIndex;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容