Board logo

标题: HMSegmentedControl源代码解析(1) [打印本页]

作者: look_w    时间: 2019-2-19 17:33     标题: HMSegmentedControl源代码解析(1)

HMSegmentedControl是一款选项卡的三方框架,github上有3000星左右。
研究了一下源码,并添加了一点注释。

HMSegmentedControl基于UIControl,上面有一个UIScrollView。
文字,图片,指示器都是用CALayer,UIBezierPath处理创建的。

    //
    //  HMSegmentedControl.h
    //  HMSegmentedControl
    //
    //  Created by Hesham Abd-Elmegid on 23/12/12.
    //  Copyright (c) 2012-2015 Hesham Abd-Elmegid. All rights reserved.
    //
     
    #import <UIKit/UIKit.h>
     
    @class HMSegmentedControl;
     
    //点击按钮的block
    typedef void (^IndexChangeBlock)(NSInteger index);
     
    //Title富文本
    typedef NSAttributedString *(^HMTitleFormatterBlock)(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected);
     
    //指示器的样式
    typedef NS_ENUM(NSInteger, HMSegmentedControlSelectionStyle) {
        //指示器和文字同宽
        HMSegmentedControlSelectionStyleTextWidthStripe, // Indicator width will only be as big as the text width
        //指示器和segment同宽
        HMSegmentedControlSelectionStyleFullWidthStripe, // Indicator width will fill the whole segment
        //矩形指示器
        HMSegmentedControlSelectionStyleBox, // A rectangle that covers the whole segment
        //箭头指示器
        HMSegmentedControlSelectionStyleArrow // An arrow in the middle of the segment pointing up or down depending on `HMSegmentedControlSelectionIndicatorLocation`
    };
     
    //指示器位置样式
    typedef NS_ENUM(NSInteger, HMSegmentedControlSelectionIndicatorLocation) {
        //上
        HMSegmentedControlSelectionIndicatorLocationUp,
        //下
        HMSegmentedControlSelectionIndicatorLocationDown,
        //无
        HMSegmentedControlSelectionIndicatorLocationNone // No selection indicator
    };
     
    typedef NS_ENUM(NSInteger, HMSegmentedControlSegmentWidthStyle) {
        //宽度fix
        HMSegmentedControlSegmentWidthStyleFixed, // Segment width is fixed
        //segment的宽度和text的宽度一致
        HMSegmentedControlSegmentWidthStyleDynamic, // Segment width will only be as big as the text width (including inset)
    };
     
    //边框类型
    typedef NS_OPTIONS(NSInteger, HMSegmentedControlBorderType) {
        HMSegmentedControlBorderTypeNone = 0,
        HMSegmentedControlBorderTypeTop = (1 << 0),
        HMSegmentedControlBorderTypeLeft = (1 << 1),
        HMSegmentedControlBorderTypeBottom = (1 << 2),
        HMSegmentedControlBorderTypeRight = (1 << 3)
    };
     
    enum {
        HMSegmentedControlNoSegment = -1   // Segment index for no selected segment
    };
     
    //segment类型
    typedef NS_ENUM(NSInteger, HMSegmentedControlType) {
        //文本
        HMSegmentedControlTypeText,
        //图片
        HMSegmentedControlTypeImages,
        //图文
        HMSegmentedControlTypeTextImages
    };
     
    @interface HMSegmentedControl : UIControl




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0