HMSegmentedControl 一个好用的标题控制器

代码不多。就两个文件。接下来,让我来未大家解析一下。如有不妥,多谢批评!

先上.h文件吧。

//
//  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;  //提前引用自己,避免循环引用  

typedef void (^IndexChangeBlock)(NSInteger index); //一个选中block
typedef NSAttributedString *(^HMTitleFormatterBlock)(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected);//富文本block
//枚举  选中的类型
typedef NS_ENUM(NSInteger, HMSegmentedControlSelectionStyle) {
   //选中宽度仅仅和文本宽度大小一样  默认是下横线
    HMSegmentedControlSelectionStyleTextWidthStripe, // Indicator width will only be as big as the text width   
  //选中宽度和文本宽度+间距一样大   默认是下横线
    HMSegmentedControlSelectionStyleFullWidthStripe, // Indicator width will fill the whole segment
//选中类型是盒子
    HMSegmentedControlSelectionStyleBox, // A rectangle that covers the whole segment
//选中类型是三角形  仅在 HMSegmentedControlSelectionIndicatorLocation有效
    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  
};
//SegmentWidthStyle 
typedef NS_ENUM(NSInteger, HMSegmentedControlSegmentWidthStyle) {
  //宽度固定
    HMSegmentedControlSegmentWidthStyleFixed, // Segment width is fixed 
  //宽度动态
    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
};

typedef NS_ENUM(NSInteger, HMSegmentedControlType) {
    HMSegmentedControlTypeText,//文本
    HMSegmentedControlTypeImages,//图片
    HMSegmentedControlTypeTextImages //文本和图片
};
//图片与文本的位置关系
typedef NS_ENUM(NSInteger, HMSegmentedControlImagePosition) {
    HMSegmentedControlImagePositionBehindText,//在文本后面
    HMSegmentedControlImagePositionLeftOfText,//在文本左边
    HMSegmentedControlImagePositionRightOfText,//在文本右边
    HMSegmentedControlImagePositionAboveText,//在文本上面
    HMSegmentedControlImagePositionBelowText//在文本下面
};

//继承自UIControl.意味着可以响应事件
@interface HMSegmentedControl : UIControl

@property (nonatomic, strong) CALayer *selectionIndicatorStripLayer;//长条layer

@property (nonatomic, strong) NSArray<NSString *> *sectionTitles;//标题
@property (nonatomic, strong) NSArray<UIImage *> *sectionImages;//图片
@property (nonatomic, strong) NSArray<UIImage *> *sectionSelectedImages;// 选中图片

/**
变化block
 Provide a block to be executed when selected index is changed.
 
 Alternativly, you could use `addTarget:action:forControlEvents:`
 */
@property (nonatomic, copy) IndexChangeBlock indexChangeBlock;

/**
自定义富文本在设置时
 Used to apply custom text styling to titles when set.
 
 When this block is set, no additional styling is applied to the `NSAttributedString` object returned from this block.
 */
@property (nonatomic, copy) HMTitleFormatterBlock titleFormatter;

/**
 Text attributes to apply to item title text.
 */
@property (nonatomic, strong) NSDictionary *titleTextAttributes UI_APPEARANCE_SELECTOR;

/*
 Text attributes to apply to selected item title text.
 
 Attributes not set in this dictionary are inherited from `titleTextAttributes`.
 */
@property (nonatomic, strong) NSDictionary *selectedTitleTextAttributes UI_APPEARANCE_SELECTOR;

/**
//背景颜色
 Segmented control background color.

 Default is `[UIColor whiteColor]`
 */
@property (nonatomic, strong) UIColor *backgroundColor UI_APPEARANCE_SELECTOR;

/**
//下划线颜色
 Color for the selection indicator stripe
 
 Default is `R:52, G:181, B:229`
 */
@property (nonatomic, strong) UIColor *selectionIndicatorColor UI_APPEARANCE_SELECTOR;

/**
//边框颜色
 Color for the selection indicator box
 
 Default is selectionIndicatorColor
 */
@property (nonatomic, strong) UIColor *selectionIndicatorBoxColor UI_APPEARANCE_SELECTOR;

/**
//分割线颜色
 Color for the vertical divider between segments.
 
 Default is `[UIColor blackColor]`
 */
@property (nonatomic, strong) UIColor *verticalDividerColor UI_APPEARANCE_SELECTOR;

/**
//边框透明度 
 Opacity for the seletion indicator box.
 
 Default is `0.2f`
 */
@property (nonatomic) CGFloat selectionIndicatorBoxOpacity;

/**
//边框宽度
 Width the vertical divider between segments that is added when `verticalDividerEnabled` is set to YES.
 
 Default is `1.0f`
 */
@property (nonatomic, assign) CGFloat verticalDividerWidth;

/**
//内容样式
 Specifies the style of the control
 
 Default is `HMSegmentedControlTypeText`
 */
@property (nonatomic, assign) HMSegmentedControlType type;

/**
//选中样式
 Specifies the style of the selection indicator.
 
 Default is `HMSegmentedControlSelectionStyleTextWidthStripe`
 */
@property (nonatomic, assign) HMSegmentedControlSelectionStyle selectionStyle;

/**
//宽度样式
 Specifies the style of the segment's width.
 
 Default is `HMSegmentedControlSegmentWidthStyleFixed`
 */
@property (nonatomic, assign) HMSegmentedControlSegmentWidthStyle segmentWidthStyle;

/**
//位置样式
 Specifies the location of the selection indicator.
 
 Default is `HMSegmentedControlSelectionIndicatorLocationUp`
 */
@property (nonatomic, assign) HMSegmentedControlSelectionIndicatorLocation selectionIndicatorLocation;

/*
//边框样式
 Specifies the border type.
 
 Default is `HMSegmentedControlBorderTypeNone`
 */
@property (nonatomic, assign) HMSegmentedControlBorderType borderType;

/**
//图片位置
 Specifies the image position relative to the text. Only applicable for HMSegmentedControlTypeTextImages
 
 Default is `HMSegmentedControlImagePositionBehindText`
 */
@property (nonatomic) HMSegmentedControlImagePosition imagePosition;

/**
//文本和图片的间距
 Specifies the distance between the text and the image. Only applicable for HMSegmentedControlTypeTextImages
 
 Default is `0,0`
 */
@property (nonatomic) CGFloat textImageSpacing;

/**
//边框颜色
 Specifies the border color.
 
 Default is `[UIColor blackColor]`
 */
@property (nonatomic, strong) UIColor *borderColor;

/**
//边框宽度
 Specifies the border width.
 
 Default is `1.0f`
 */
@property (nonatomic, assign) CGFloat borderWidth;

/**
//是否能滚动
 Default is YES. Set to NO to deny scrolling by dragging the scrollView by the user.
 */
@property(nonatomic, getter = isUserDraggable) BOOL userDraggable;

/**
//是否能响应事件
 Default is YES. Set to NO to deny any touch events by the user.
 */
@property(nonatomic, getter = isTouchEnabled) BOOL touchEnabled;

/**
//是否能被分割
 Default is NO. Set to YES to show a vertical divider between the segments.
 */
@property(nonatomic, getter = isVerticalDividerEnabled) BOOL verticalDividerEnabled;

@property (nonatomic, getter=shouldStretchSegmentsToScreenSize) BOOL stretchSegmentsToScreenSize;

/**
//下标是什么
 Index of the currently selected segment.
 */
@property (nonatomic, assign) NSInteger selectedSegmentIndex;

/**
//选中的高度
 Height of the selection indicator. Only effective when `HMSegmentedControlSelectionStyle` is either `HMSegmentedControlSelectionStyleTextWidthStripe` or `HMSegmentedControlSelectionStyleFullWidthStripe`.
 
 Default is 5.0
 */
@property (nonatomic, readwrite) CGFloat selectionIndicatorHeight;

/**
//选中的间距
 Edge insets for the selection indicator.
 NOTE: This does not affect the bounding box of HMSegmentedControlSelectionStyleBox
 
 When HMSegmentedControlSelectionIndicatorLocationUp is selected, bottom edge insets are not used
 
 When HMSegmentedControlSelectionIndicatorLocationDown is selected, top edge insets are not used
 
 Defaults are top: 0.0f
             left: 0.0f
           bottom: 0.0f
            right: 0.0f
 */
@property (nonatomic, readwrite) UIEdgeInsets selectionIndicatorEdgeInsets;

/**
//segment的间距
 Inset left and right edges of segments.
 
 Default is UIEdgeInsetsMake(0, 5, 0, 5)
 */
@property (nonatomic, readwrite) UIEdgeInsets segmentEdgeInset;
//扩大响应事件的间距
@property (nonatomic, readwrite) UIEdgeInsets enlargeEdgeInset;

/**
//是否加动画
 Default is YES. Set to NO to disable animation during user selection.
 */
@property (nonatomic) BOOL shouldAnimateUserSelection;
//初始化方法
- (id)initWithSectionTitles:(NSArray<NSString *> *)sectiontitles;
- (id)initWithSectionImages:(NSArray<UIImage *> *)sectionImages sectionSelectedImages:(NSArray<UIImage *> *)sectionSelectedImages;
- (instancetype)initWithSectionImages:(NSArray<UIImage *> *)sectionImages sectionSelectedImages:(NSArray<UIImage *> *)sectionSelectedImages titlesForSections:(NSArray<NSString *> *)sectiontitles;
- (void)setSelectedSegmentIndex:(NSUInteger)index animated:(BOOL)animated;
- (void)setIndexChangeBlock:(IndexChangeBlock)indexChangeBlock;
- (void)setTitleFormatter:(HMTitleFormatterBlock)titleFormatter;

@end





感谢看到最后。对,这就是最后了。.m就不解析了.毕竟这个是第三方库。会用就好了,如果对源码真的感兴趣,自己可以去看.m。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,300评论 4 61
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,172评论 1 32
  • “教育要学生带走的不仅是书包里的东西,还有超越书本知识的人的素养。” “人们在掌握知识时,如果没有理解意义,那么,...
    小树临风刘祥波阅读 1,994评论 0 10
  • echo "show processlist;"| mysql -hxxx -Pxxx -uxxx -D xxx ...
    cybersword阅读 489评论 0 0
  • 秋天越走越深,身上一阵阵发紧,手上脸上干燥的难受,依然是风大,海边的风狂劲粗暴,带着海的腥,把你身体仅有的水分吹起...
    启航2016阅读 302评论 0 1