IOS開發--自訂segment控制項,方便自訂樣式,iossegment

來源:互聯網
上載者:User

IOS開發--自訂segment控制項,方便自訂樣式,iossegment

系統的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

 

 

實現檔案:

#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觸發,否則會造成重複執行        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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.