Board logo

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

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

@interface HMSegmentedControl ()

//3中不同的指示器的layer
@property (nonatomic, strong) CALayer *selectionIndicatorStripLayer;
@property (nonatomic, strong) CALayer *selectionIndicatorBoxLayer;
@property (nonatomic, strong) CALayer *selectionIndicatorArrowLayer;

@property (nonatomic, readwrite) CGFloat segmentWidth;
@property (nonatomic, readwrite) NSArray<NSNumber *> *segmentWidthsArray;
@property (nonatomic, strong) HMScrollView *scrollView;

@end

@implementation HMSegmentedControl

#pragma mark - 初始化
- (id)initWithCoderNSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (id)initWithFrameCGRect)frame {
    self = [super initWithFrame:frame];
   
    if (self) {
        [self commonInit];
    }
   
    return self;
}

- (id)initWithSectionTitlesNSArray<NSString *> *)sectiontitles {
    self = [super initWithFrame:CGRectZero];
   
    if (self) {
        [self commonInit];
        self.sectionTitles = sectiontitles;
        self.type = HMSegmentedControlTypeText;
    }
   
    return self;
}

- (id)initWithSectionImagesNSArray<UIImage *> *)sectionImages sectionSelectedImagesNSArray<UIImage *> *)sectionSelectedImages {
    self = [super initWithFrame:CGRectZero];
   
    if (self) {
        [self commonInit];
        self.sectionImages = sectionImages;
        self.sectionSelectedImages = sectionSelectedImages;
        self.type = HMSegmentedControlTypeImages;
    }
   
    return self;
}

- (instancetype)initWithSectionImagesNSArray<UIImage *> *)sectionImages sectionSelectedImagesNSArray<UIImage *> *)sectionSelectedImages titlesForSectionsNSArray<NSString *> *)sectiontitles {
    self = [super initWithFrame:CGRectZero];
   
    if (self) {
        [self commonInit];
        
        if (sectionImages.count != sectiontitles.count) {
            //报错信息
            [NSException raise:NSRangeException format"***%s: Images bounds (%ld) Don't match Title bounds (%ld)", sel_getName(_cmd), (unsigned long)sectionImages.count, (unsigned long)sectiontitles.count];
        }
        
        self.sectionImages = sectionImages;
        self.sectionSelectedImages = sectionSelectedImages;
        self.sectionTitles = sectiontitles;
        self.type = HMSegmentedControlTypeTextImages;
    }
   
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
   
    self.segmentWidth = 0.0f;
}

#pragma mark - createView

/**
初始化界面与参数
*/
- (void)commonInit {
    //创建底层scrollview
    self.scrollView = [[HMScrollView alloc] init];
    self.scrollView.scrollsToTop = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.scrollView];
   
    _backgroundColor = [UIColor whiteColor];
   
    //opaque也是表示当前的UIView的不透明度,设置是否之后对于UIView的显示并没有什么影响,官方文档的意思简单点说就是opaque默认为YES,如果alpha小于1,那么应该设置opaque设置为NO,但是如果alpha为1,opaque设置为NO,产生的后果是不可预料的~
    self.opaque = NO;
   
    //设置指示器背景颜色
    _selectionIndicatorColor = [UIColor colorWithRed:52.0f/255.0f green:181.0f/255.0f blue:229.0f/255.0f alpha:1.0f];
    _selectionIndicatorBoxColor = _selectionIndicatorColor;

    //默认选中0
    self.selectedSegmentIndex = 0;
    self.segmentEdgeInset = UIEdgeInsetsMake(0, 5, 0, 5);
    self.selectionIndicatorHeight = 5.0f;
    self.selectionIndicatorEdgeInsets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
    self.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    self.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationUp;
    self.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleFixed;
    self.userDraggable = YES;
    self.touchEnabled = YES;
    self.verticalDividerEnabled = NO;
    self.type = HMSegmentedControlTypeText;
    self.verticalDividerWidth = 1.0f;
    _verticalDividerColor = [UIColor blackColor];
    self.borderColor = [UIColor blackColor];
    self.borderWidth = 1.0f;
   
    self.shouldAnimateUserSelection = YES;




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