一、前言
這是第二次寫(xiě)博客。原計(jì)劃一個(gè)月一篇的博客,然后發(fā)現(xiàn)自己賊懶。變成一年一篇了.....
僅當(dāng)做是自己的記錄吧~(只能這樣安慰自己了?。?!)
二、正題
最近接到了一個(gè)需求,需要做個(gè)輪播圖。原則上來(lái)說(shuō)做個(gè)輪播很是簡(jiǎn)單的,套用個(gè)第三方或者自己使用UIScrollView + GCD 封裝一個(gè)即可。(效果如下圖)

這次我們使用github上比較火的一個(gè)第三方控件來(lái)做,然后再修改一下第三方的PageControl計(jì)算規(guī)則,即可達(dá)到效果。
先導(dǎo)入第三方的SDCycleScrollView(導(dǎo)入方式默認(rèn)大家都會(huì)了,這邊就不詳解了)。SDCycleScrollView有提供兩個(gè)自定義的底部分頁(yè)小圖標(biāo)
/** 當(dāng)前分頁(yè)控件小圓標(biāo)圖片 */
@property (nonatomic, strong) UIImage *currentPageDotImage;
/** 其他分頁(yè)控件小圓標(biāo)圖片 */
@property (nonatomic, strong) UIImage *pageDotImage;
大家只需要把想要的效果圖替換即可。
但是當(dāng)圖片喜歡進(jìn)去后,發(fā)現(xiàn)圖片并不能自適應(yīng)大小,而是被壓縮了。都變成固定大小了,這個(gè)時(shí)候就需要處理自適應(yīng)的大小了。
可直接在TAPageControl.m類(lèi)進(jìn)行處理。
NO.1 先找到- (CGSize)sizeForNumberOfPages:(NSInteger)pageCount方法,修改PageControl的總寬高.
- (CGSize)sizeForNumberOfPages:(NSInteger)pageCount
{
return CGSizeMake((self.dotSize.width + self.spacingBetweenDots) * pageCount - self.spacingBetweenDots + self.currentDotImage.size.width, self.currentDotImage.size.height);
}
NO.2 總寬高設(shè)置完后,需要設(shè)置動(dòng)態(tài)滾動(dòng)起來(lái)后的效果;找到- (void)changeActivity:(BOOL)active atIndex:(NSInteger)index,修改此方法
/**
* Change activity state of a dot view. Current/not currrent.
*
* @param active Active state to apply
* @param index Index of dot for state update
*/
- (void)changeActivity:(BOOL)active atIndex:(NSInteger)index
{
if (self.dotViewClass) {
TAAbstractDotView *abstractDotView = (TAAbstractDotView *)[self.dots objectAtIndex:index];
if ([abstractDotView respondsToSelector:@selector(changeActivityState:)]) {
[abstractDotView changeActivityState:active];
} else {
NSLog(@"Custom view : %@ must implement an 'changeActivityState' method or you can subclass %@ to help you.", self.dotViewClass, [TAAbstractDotView class]);
}
} else if (self.dotImage && self.currentDotImage) {
UIImageView *dotView = (UIImageView *)[self.dots objectAtIndex:index];
dotView.image = (active) ? self.currentDotImage : self.dotImage;
[self updateDotFrame:dotView atIndex:index];
for (int i = 0; i < self.numberOfPages; i++) {
UIImageView *tempDotView = (UIImageView *)[self.dots objectAtIndex:i];
[self updateDotFrame:tempDotView atIndex:i];
}
}
}
NO.3設(shè)置完滾動(dòng)的效果,還需要話(huà)處理連帶的效果處理,更新的方法:- (void)updateDotFrame:(UIView *)dot atIndex:(NSInteger)index,此方法是對(duì)每個(gè)小圓點(diǎn)的位置都重新賦值
/**
* Update the frame of a specific dot at a specific index
*
* @param dot Dot view
* @param index Page index of dot
*/
- (void)updateDotFrame:(UIView *)dot atIndex:(NSInteger)index
{
CGFloat x = (self.dotSize.width + self.spacingBetweenDots) * index + ( (CGRectGetWidth(self.frame) - [self sizeForNumberOfPages:self.numberOfPages].width) / 2);
CGFloat y = (CGRectGetHeight(self.frame) - self.dotSize.height) / 2;
if (index > self.currentPage) {
x = (self.dotSize.width + self.spacingBetweenDots) * index + ( (CGRectGetWidth(self.frame) - [self sizeForNumberOfPages:self.numberOfPages].width) / 2) +self.currentDotImage.size.width - self.dotSize.width;
}
if (index == self.currentPage) {
dot.frame = CGRectMake(x, 0, self.currentDotImage.size.width, self.currentDotImage.size.height);
}else{
dot.frame = CGRectMake(x, y, self.dotSize.width, self.dotSize.height);
}
}
至此,產(chǎn)品需要的需求效果已經(jīng)完成。