美文网首页ios开发
一个比较好用的第三方分段控制器HMSegmentedContro

一个比较好用的第三方分段控制器HMSegmentedContro

作者: 蒋昉霖 | 来源:发表于2016-03-31 10:50 被阅读5755次

类似网易新闻的首页等,这种东西还是比较常用的,总结了一下提供的外部属性和方法,基本能满足日常开发

简单记录一下吧,对属性进行基本测试吧,也不写别的了,官方Demo都有

#import <UIKit/UIKit.h>

@class HMSegmentedControl;

typedef void (^IndexChangeBlock)(NSInteger index);
typedef NSAttributedString *(^HMTitleFormatterBlock)(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected);

typedef enum {
    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 // 点击之后按钮背景会有凹陷效果
    HMSegmentedControlSelectionStyleArrow // An arrow in the middle of the segment pointing up or down depending on `HMSegmentedControlSelectionIndicatorLocation` // 选中出现个三角,枚举值,顶部出现,下部出现,不出现
} HMSegmentedControlSelectionStyle; // 选中样式

typedef enum {
    HMSegmentedControlSelectionIndicatorLocationUp, // 顶上出现
    HMSegmentedControlSelectionIndicatorLocationDown, // 底部出现
    HMSegmentedControlSelectionIndicatorLocationNone // No selection indicator // 不出现
} HMSegmentedControlSelectionIndicatorLocation; // 选中出现一个三角

typedef enum {
    HMSegmentedControlSegmentWidthStyleFixed, // Segment width is fixed // 自动填充整个宽度
    HMSegmentedControlSegmentWidthStyleDynamic, // Segment width will only be as big as the text width (including inset) // 根据最大文字算出宽度,自适应
} HMSegmentedControlSegmentWidthStyle; // 设置按钮大小样式的

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 enum {
    HMSegmentedControlTypeText,
    HMSegmentedControlTypeImages,
    HMSegmentedControlTypeTextImages
} HMSegmentedControlType; // 选择按钮放的东西

@interface HMSegmentedControl : UIControl

@property (nonatomic, strong) NSArray *sectionTitles;
@property (nonatomic, strong) NSArray *sectionImages;
@property (nonatomic, strong) NSArray *sectionSelectedImages;

/**
 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/box
 
 Default is `R:52, G:181, B:229`
 */
@property (nonatomic, strong) UIColor *selectionIndicatorColor 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 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; // 分割线显示还是不显示

/**
 Index of the currently selected segment.
 */
@property (nonatomic, assign) NSInteger selectedSegmentIndex; // 当前选择的标号,从0开始

/**
 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; // 选择框包边

/**
 Inset left and right edges of segments.
 
 Default is UIEdgeInsetsMake(0, 5, 0, 5)
 */
@property (nonatomic, readwrite) UIEdgeInsets segmentEdgeInset; // 插图左派和右派的边缘部分。

/**
 Default is YES. Set to NO to disable animation during user selection.
 */
@property (nonatomic) BOOL shouldAnimateUserSelection; // 选择变动的动画

- (id)initWithSectionTitles:(NSArray *)sectiontitles;
- (id)initWithSectionImages:(NSArray *)sectionImages sectionSelectedImages:(NSArray *)sectionSelectedImages;
- (instancetype)initWithSectionImages:(NSArray *)sectionImages sectionSelectedImages:(NSArray *)sectionSelectedImages titlesForSections:(NSArray *)sectiontitles;
- (void)setSelectedSegmentIndex:(NSUInteger)index animated:(BOOL)animated;
- (void)setIndexChangeBlock:(IndexChangeBlock)indexChangeBlock;
- (void)setTitleFormatter:(HMTitleFormatterBlock)titleFormatter;

相关文章

网友评论

  • 文刀三三:我们的变态产品要求 下面那个横线宽度是100.。。好像没有自定义的选项啊
    蒋昉霖:@文刀三三 了不起!
    文刀三三:@蒋昉霖 我看他属性里有个根据按钮文字长度设置横线长度的,我就把文字后面加了些空格哈哈0.0
    蒋昉霖:...你要么自己写个,也不难,要么,你就把他的代码下本地,然后进去把源码改了,这个逻辑应该好找
  • 李某lkb:有这个动画吗?下面的线跟随滑动的距离变化.
    蒋昉霖:记不清了。。。现在很少用这些第三方了,sorry
  • fe2cd300a05a:就想知道 iOS 10 怎么让显示!!
    God_Dong:https://github.com/HeshamMegid/HMSegmentedControl在这里下载更新就行
  • 代码:标题的字体大小怎么设置啊!
    童话镇里蜿蜒的河:@代码
    UIFont* font = [UIFont systemFontOfSize:15];
    UIColor* nomalColor = UIColorFromRGB(0x9a9a9a);

    _segmentedControl.titleTextAttributes = @{NSFontAttributeName:font,
    NSForegroundColorAttributeName:nomalColor};
  • 但求今生不愿来世:升级到ios 10 之后,这个控件的标题消失了是什么情况 能不能救个急 谢谢
    黑旭鹏:@没有酱帅 设置标题可以解决吗
    没有酱帅:可以设置标题的一些属性
    没有酱帅:@但求今生不愿来世 方法没了,他们貌似还没做适配,我用的这个HMSegmentedControl@hons82

本文标题:一个比较好用的第三方分段控制器HMSegmentedContro

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