擼了一個(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;
}