国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

IOS開發(fā)--自定義segment控件,方便自定義樣式

2019-11-14 19:43:15
字體:
供稿:網(wǎng)友

系統(tǒng)的segment控件太封閉,想換個顏色加個背景太難了,忍不住自己寫一個,以備不時之需

這個控件給出了很多自定義屬性的設置,用起來還是比較方便的,需要注意的 itemWidth如果不設置,則會按照控件的寬度平均分配每一項的寬度,如果設置了,那么總寬度超過控件寬度后會有滑動效果

直接上代碼吧:

頭文件:

#import <Foundation/Foundation.h>@PRotocol WCSegmentControlDelegate-(void)wcSegmentControlSelectionChanged:(id)sender;@end@interface WCSegmentControl : UIView@property (nonatomic, strong)id<WCSegmentControlDelegate>delegate;@property (nonatomic, strong) NSMutableArray *dataSourceOFTitle;@property (nonatomic, assign, setter=setCurrentSelectedIndex:) int currentSelectedIndex;//title color@property (nonatomic, strong) UIColor *titleColor;@property (nonatomic, strong) UIColor *selectedTitleColor;//font@property (nonatomic, strong) UIFont *titleFont;//item selectedBackground  Color;@property (nonatomic, strong) UIColor *itemBackgroundColor;@property (nonatomic, strong) UIColor *selectedItemBackgroundColor;//item selectedBackground image;@property (nonatomic, strong) UIImage *itemBackgroundImage;@property (nonatomic, strong) UIImage *selectedItemBackgroundImage;//item border@property (nonatomic, strong) UIColor *itemBorderColor;@property (nonatomic, assign) BOOL isShowItemBorderWhenHilight;@property (nonatomic, assign) int itemBorderWidth;@property (nonatomic, assign) int itemCornerRadius;//item, 不設置則均分控件寬度@property (nonatomic, assign) int itemWidth;//control border@property (nonatomic, strong) UIColor *borderColor;@property (nonatomic, assign) BOOL isShowBorder;@property (nonatomic, assign) int borderWidth;@property (nonatomic, assign) int cornerRadius;//分割線@property (nonatomic, strong) UIColor *splitColor;@property (nonatomic, assign) int splitBorderWidth;@property (nonatomic, assign) BOOL isShowSplitBorder;@end

 

 

實現(xiàn)文件:

#import "WCSegmentControl.h"#import "WCSegmentControlItemButton.h"@implementation WCSegmentControl {    UIScrollView *_scrollView;}- (id)initWithFrame:(CGRect)frame {    self = [super initWithFrame:frame];    if (self) {        self.clipsToBounds = YES;        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];        _scrollView.backgroundColor = self.backgroundColor;        [self addSubview:_scrollView];        _dataSourceOFTitle = [NSMutableArray array];        _selectedTitleColor = [UIColor whiteColor];        _titleColor = [UIColor blackColor];        _itemBackgroundColor = [UIColor whiteColor];        _selectedItemBackgroundColor = kWCColor7;        _borderWidth = 1;        _borderColor = kWCColor7;        _cornerRadius = 5;        _itemBorderColor = kWCColor7;        _itemBorderWidth = 1;        _itemCornerRadius = 0;        _titleFont = [UIFont systemFontOfSize:12];        _splitColor = kWCColor7;        _splitBorderWidth = 1;        _isShowBorder = YES;        _isShowItemBorderWhenHilight = NO;        _isShowSplitBorder = YES;    }    return self;}- (void)setDataSourceOFTitle:(NSMutableArray *)dataSourceOFTitle {    _dataSourceOFTitle = dataSourceOFTitle;    [self reloadData];}-(WCSegmentControlItemButton *)createItemControlWithTitle:(NSString *)title {    WCSegmentControlItemButton * btn = [[WCSegmentControlItemButton alloc] init];    if(_itemBackgroundImage)    {        [btn setBackgroundImage:_itemBackgroundImage forState:UIControlStateNormal];    }    if(_selectedItemBackgroundImage)    {        [btn setBackgroundImage:_selectedItemBackgroundImage forState:UIControlStateSelected];    }    [btn setBackgroundColor:_itemBackgroundColor];    [btn setTitleColor:_selectedTitleColor forState:UIControlStateSelected];    [btn setTitleColor:_titleColor forState:UIControlStateNormal];    btn.titleLabel.font = _titleFont;    [btn setTitle:title forState:UIControlStateNormal];    btn.layer.cornerRadius = _itemCornerRadius;    return btn;}- (void)refreshUI {    if (_isShowBorder) {        self.layer.borderWidth = _borderWidth;        self.layer.borderColor = _borderColor.CGColor;        self.layer.cornerRadius = _cornerRadius;    } else {        self.layer.borderWidth = 0;    }}    -(void) reloadData    {        [_scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];        if ([_dataSourceOFTitle count] > 0) {//        UIEdgeInsets            CGRect fra = CGRectMake(                    0,                    0,                    _itemWidth > 0 ? _itemWidth : self.width / [_dataSourceOFTitle count],                    self.height);            CGFloat leftMargin = MAX(0, (self.width -fra.size.width* [self.dataSourceOFTitle count])/2 );            __block CGFloat contentWidth = leftMargin;            [self.dataSourceOFTitle enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL *stop) {                WCSegmentControlItemButton *btn = [self createItemControlWithTitle:title];                btn.frame = fra;                btn.left =leftMargin + idx * btn.width;                [btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];                btn.index = idx;                [_scrollView addSubview:btn];                if (_isShowSplitBorder && idx != 0) {                     UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _splitBorderWidth, btn.height)];                    line.backgroundColor = _splitColor;                    line.left = btn.left;                    [_scrollView addSubview:line];                }                contentWidth = btn.right;            }];            _scrollView.contentSize = CGSizeMake(contentWidth, _scrollView.height);            [self setCurrentSelectedIndex:0];            [self refreshUI];        }    }    -(void) btnTapped:(id) sender    {        WCSegmentControlItemButton *btn = sender;        if (self.currentSelectedIndex == btn.index) {            return;        }        [self setCurrentSelectedIndex:btn.index];        //不可以在setCurrentSelectedIndex觸發(fā),否則會造成重復執(zhí)行        if ([(NSObject *) (self.delegate) respondsToSelector:@selector(wcSegmentControlSelectionChanged:)]) {            [self.delegate wcSegmentControlSelectionChanged:self];        }    }    -(void) setCurrentSelectedIndex:(int) currentSelectedIndex    {        _currentSelectedIndex = currentSelectedIndex;        [_scrollView.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {            if ([obj isKindOfClass:[WCSegmentControlItemButton class]]) {                WCSegmentControlItemButton *view = obj;                if (view.index == currentSelectedIndex) {                    [view setSelected:YES];                    [view setBackgroundColor:_selectedItemBackgroundColor];                    //如果在屏幕外則需要移動到屏幕中                    if (view.right - _scrollView.width > 0) {                        _scrollView.contentOffset = CGPointMake(view.right - _scrollView.width, 0)                        ;                    }else if (view.left - _scrollView.contentOffset.x < 0) {                        _scrollView.contentOffset =  CGPointMake(view.left, 0);                    }                    if (_isShowItemBorderWhenHilight) {                        view.layer.borderWidth = _borderWidth;                        view.layer.borderColor = _borderColor.CGColor;                        view.layer.cornerRadius = _cornerRadius;                    }                } else {                    [view setSelected:NO];                    [view setBackgroundColor:_itemBackgroundColor];                    view.layer.borderWidth = 0;                }            }        }];    }    @end

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 东阿县| 余江县| 奉节县| 天台县| 凤凰县| 特克斯县| 阿拉善盟| 桑植县| 克拉玛依市| 阿克陶县| 鄂托克旗| 玉屏| 昌宁县| 静海县| 安远县| 黑山县| 襄垣县| 正镶白旗| 冀州市| 马尔康县| 曲松县| 沾化县| 行唐县| 安福县| 新津县| 咸宁市| 梓潼县| 镇原县| 临西县| 北票市| 潮安县| 象州县| 永宁县| 惠来县| 灯塔市| 开封县| 舒兰市| 岳阳市| 宽城| 大港区| 金坛市|