HMSegmentedControl.h 7.99 KB
//
//  BBAHMSegmentedControl.h
//  BBAHMSegmentedControl
//
//  Created by Hesham Abd-Elmegid on 23/12/12.
//  Copyright (c) 2012-2015 Hesham Abd-Elmegid. All rights reserved.
//
/******************************************************************************/
// 描述:Sprint7.0引入 github:https://github.com/HeshamMegid/HMSegmentedControl
/******************************************************************************/

#import <UIKit/UIKit.h>

@class BBAHMSegmentedControl;

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

typedef enum {
    BBAHMSegmentedControlSelectionStyleTextWidthStripe, // Indicator width will only be as big as the text width
    BBAHMSegmentedControlSelectionStyleFullWidthStripe, // Indicator width will fill the whole segment
    BBAHMSegmentedControlSelectionStyleBox, // A rectangle that covers the whole segment
    BBAHMSegmentedControlSelectionStyleArrow // An arrow in the middle of the segment pointing up or down depending on `BBAHMSegmentedControlSelectionIndicatorLocation`
} BBAHMSegmentedControlSelectionStyle;

typedef enum {
    BBAHMSegmentedControlSelectionIndicatorLocationUp,
    BBAHMSegmentedControlSelectionIndicatorLocationDown,
	BBAHMSegmentedControlSelectionIndicatorLocationNone // No selection indicator
} BBAHMSegmentedControlSelectionIndicatorLocation;

typedef enum {
    BBAHMSegmentedControlSegmentWidthStyleFixed, // Segment width is fixed
    BBAHMSegmentedControlSegmentWidthStyleDynamic, // Segment width will only be as big as the text width (including inset)
} BBAHMSegmentedControlSegmentWidthStyle;

typedef NS_OPTIONS(NSInteger, BBAHMSegmentedControlBorderType) {
    BBAHMSegmentedControlBorderTypeNone = 0,
    BBAHMSegmentedControlBorderTypeTop = (1 << 0),
    BBAHMSegmentedControlBorderTypeLeft = (1 << 1),
    BBAHMSegmentedControlBorderTypeBottom = (1 << 2),
    BBAHMSegmentedControlBorderTypeRight = (1 << 3)
};

enum {
    BBAHMSegmentedControlNoSegment = -1   // Segment index for no selected segment
};

typedef enum {
    BBAHMSegmentedControlTypeText,
    BBAHMSegmentedControlTypeImages,
	BBAHMSegmentedControlTypeTextImages
} BBAHMSegmentedControlType;

__attribute__((deprecated("请使用BBAMultiTabBar")))
@interface BBAHMSegmentedControl : 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) BBAHMTitleFormatterBlock 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 `BBAHMSegmentedControlTypeText`
 */
@property (nonatomic, assign) BBAHMSegmentedControlType type;

/**
 Specifies the style of the selection indicator.
 
 Default is `BBAHMSegmentedControlSelectionStyleTextWidthStripe`
 */
@property (nonatomic, assign) BBAHMSegmentedControlSelectionStyle selectionStyle;

/**
 Specifies the style of the segment's width.
 
 Default is `BBAHMSegmentedControlSegmentWidthStyleFixed`
 */
@property (nonatomic, assign) BBAHMSegmentedControlSegmentWidthStyle segmentWidthStyle;

/**
 Specifies the location of the selection indicator.
 
 Default is `BBAHMSegmentedControlSelectionIndicatorLocationUp`
 */
@property (nonatomic, assign) BBAHMSegmentedControlSelectionIndicatorLocation selectionIndicatorLocation;

/*
 Specifies the border type.
 
 Default is `BBAHMSegmentedControlBorderTypeNone`
 */
@property (nonatomic, assign) BBAHMSegmentedControlBorderType 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;

/**
 Height of the selection indicator. Only effective when `BBAHMSegmentedControlSelectionStyle` is either `BBAHMSegmentedControlSelectionStyleTextWidthStripe` or `BBAHMSegmentedControlSelectionStyleFullWidthStripe`.
 
 Default is 5.0
 */
@property (nonatomic, readwrite) CGFloat selectionIndicatorHeight;

/**
 Edge insets for the selection indicator.
 NOTE: This does not affect the bounding box of BBAHMSegmentedControlSelectionStyleBox
 
 When BBAHMSegmentedControlSelectionIndicatorLocationUp is selected, bottom edge insets are not used
 
 When BBAHMSegmentedControlSelectionIndicatorLocationDown 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)setSelectedSegmentIndex:(NSUInteger)index animated:(BOOL)animated notify:(BOOL)notify;
- (void)setIndexChangeBlock:(IndexChangeBlock)indexChangeBlock;
- (void)setTitleFormatter:(BBAHMTitleFormatterBlock)titleFormatter;

- (void)updateBadgeNumber:(NSUInteger)badge atIndex:(NSUInteger)index;//add by guojin 20151124,sprint7.0
//10.13 增加segment文字带有小红点,注意此处的飘新逻辑:为了数字飘新和红点飘新共存,
//所以当badge>0则是数字飘新,badge=0时则小红点飘新,badge=-1才是清除飘新,使用方要注意
@property (nonatomic, strong) NSMutableArray *badgeArray;
@end