WMPageController选项卡自定义图片

项目需求如下图:

  • 修改选项卡为自定义图片
  • 加粗选中的字体
  • WMPageController.gif
查看 WMPageController (版本2.5.2)源代码,感谢作者开源,验证其最后都在WMProgressView.m中的- (void)drawRect:(CGRect)rect进行重绘。
故仿照其重绘过程,增加自定义绘制在WMProgressView.h
...
@property (nonatomic, assign) BOOL hollow;
@property (nonatomic, assign) BOOL hasBorder;
@property (nonatomic, assign) BOOL strokeImage; //增加填充图片Bool值,根据此值来进行绘制

- (void)setProgressWithOutAnimate:(CGFloat)progress;

WMProgressView.m中的drawRect:方法中增加绘制方式,同时导入图片资源。

    if (self.strokeImage) {
        UIImage *image = [UIImage imageNamed:@"jy_healthInfo_ic_titleBG"];
        //翻转镜像
        CGContextTranslateCTM(ctx, 0, height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        //画图
        CGContextDrawImage(ctx, CGRectMake(startX+10, lineWidth / 2.0, width-20, height - lineWidth), image.CGImage);
        return;
    }

跟踪其hollow、hasBorder查看赋值的地方,发现在addProgressViewWithFrame:::::
所以更新这个方法

- (void)addProgressViewWithFrame:(CGRect)frame isTriangle:(BOOL)isTriangle hasBorder:(BOOL)hasBorder hollow:(BOOL)isHollow strokeImage:(BOOL)strokeImage cornerRadius:(CGFloat)cornerRadius {
    WMProgressView *pView = [[WMProgressView alloc] initWithFrame:frame];
    pView.itemFrames = [self convertProgressWidthsToFrames];
    pView.color = self.lineColor.CGColor;
    pView.isTriangle = isTriangle;
    pView.hasBorder = hasBorder;
    pView.hollow = isHollow;
    pView.cornerRadius = cornerRadius;
    pView.naughty = self.progressViewIsNaughty;
    pView.speedFactor = self.speedFactor;
    pView.backgroundColor = [UIColor clearColor];
    pView.strokeImage = strokeImage;
    self.progressView = pView;
    [self.scrollView insertSubview:self.progressView atIndex:0];
}

添加stokeImage,并修改报错

    [self addProgressViewWithFrame:frame
                        isTriangle:(self.style == WMMenuViewStyleTriangle)
                         hasBorder:(self.style == WMMenuViewStyleSegmented)
                            hollow:(self.style == WMMenuViewStyleFloodHollow)
                       strokeImage:(self.style == WMMenuViewStyleStrokeImage)
                      cornerRadius:self.progressViewCornerRadius];

WMMenuView.h中,增加枚举值

typedef NS_ENUM(NSUInteger, WMMenuViewStyle) {
    ...
    WMMenuViewStyleSegmented,    // 涌入带边框,即网易新闻选项卡
    WMMenuViewStyleStrokeImage,  // 添加自定义图片
};

在初始化WMPageController控制器的时候,将menuViewStyle属性赋值为新增的枚举值self.pageController.menuViewStyle = WMMenuViewStyleStrokeImage;

测试已经可以显示图片了。

最后,找到字体赋值的地方,修改粗体

        if (self.fontName) {
            item.font = [UIFont fontWithName:self.fontName size:item.selectedSize];
        } else {
            //item.font = [UIFont systemFontOfSize:item.selectedSize];
            item.font = [UIFont fontWithName:@"Helvetica-Bold" size:item.selectedSize]; //选中变成粗体
        }

完成,顺道再贴个Demo地址

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,903评论 1 32
  • Quartz2D以及drawRect的重绘机制字数1487 阅读21 评论1 喜欢1一、什么是Quartz2D Q...
    PurpleWind阅读 4,272评论 0 3
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,906评论 0 17
  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 8,983评论 2 4
  • 我是双鱼座,我很认同并喜欢我的星座,大致特征就是感情心思细腻、敏感,对生活有较强的悟性,并畅游于精神世界里,...
    赤脚姑娘_瑜伽习者阅读 4,125评论 2 2

友情链接更多精彩内容