美文网首页
iOS 修改 SDCycleScrollView 的滚动图标

iOS 修改 SDCycleScrollView 的滚动图标

作者: 近听雨落声丶 | 来源:发表于2019-12-20 11:46 被阅读0次

    一、前言
    这是第二次写博客。原计划一个月一篇的博客,然后发现自己贼懒。变成一年一篇了.....
    仅当做是自己的记录吧~(只能这样安慰自己了!!!)

    二、正题
    最近接到了一个需求,需要做个轮播图。原则上来说做个轮播很是简单的,套用个第三方或者自己使用UIScrollView + GCD 封装一个即可。(效果如下图)


    01.jpg

    这次我们使用github上比较火的一个第三方控件来做,然后再修改一下第三方的PageControl计算规则,即可达到效果。

    先导入第三方的SDCycleScrollView(导入方式默认大家都会了,这边就不详解了)。SDCycleScrollView有提供两个自定义的底部分页小图标

    /** 当前分页控件小圆标图片 */
    @property (nonatomic, strong) UIImage *currentPageDotImage;
    
    /** 其他分页控件小圆标图片 */
    @property (nonatomic, strong) UIImage *pageDotImage;
    
    

    大家只需要把想要的效果图替换即可。
    但是当图片喜欢进去后,发现图片并不能自适应大小,而是被压缩了。都变成固定大小了,这个时候就需要处理自适应的大小了。
    可直接在TAPageControl.m类进行处理。

    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 总宽高设置完后,需要设置动态滚动起来后的效果;找到- (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设置完滚动的效果,还需要话处理连带的效果处理,更新的方法:- (void)updateDotFrame:(UIView *)dot atIndex:(NSInteger)index,此方法是对每个小圆点的位置都重新赋值

    /**
     *  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);
        }
    }
    

    至此,产品需要的需求效果已经完成。

    相关文章

      网友评论

          本文标题:iOS 修改 SDCycleScrollView 的滚动图标

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