美文网首页
iOS SDCycleScrollView 自定义 PageCo

iOS SDCycleScrollView 自定义 PageCo

作者: CoderLGL | 来源:发表于2019-12-09 21:27 被阅读0次

基于SDCycleScrollView对里面的PageControl进行样式自定义,自己想要什么样式就对PageControlcurrentPageDotImagepageDotImage设置相应的图片就行,视图会根据图片自适应.

以下均在TAPageControl.m中进行修改

修改流程:

    1. 改变整个PageControl的大小
      找到- (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);
}
    1. PageControl滚动时重新计算里面的每个小圆点的位置.
      每当视图滚动时都要调用- (void)changeActivity:(BOOL)active atIndex:(NSInteger)index,修改此方法
- (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];
        }
    }
}

可以和源码作对比,会发现重新调用了- (void)updateDotFrame:(UIView *)dot atIndex:(NSInteger)index,此方法是对每个小圆点的位置都重新赋值.

- (void)updateDotFrame:(UIView *)dot atIndex:(NSInteger)index
{
    // Dots are always centered within view
    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);
    }
    
    
}

到此结束!

相关文章

网友评论

      本文标题:iOS SDCycleScrollView 自定义 PageCo

      本文链接:https://www.haomeiwen.com/subject/dnvigctx.html