ios UISegmentedControl KVO 检测selectedSegmentIndex的改变

来源:互联网 发布:app软件推广 编辑:程序博客网 时间:2024/06/07 20:54

       最近做的一个项目需要用到UISegmentedControl,但是UI的效果与UISegmentedControl的效果存在一些差异,

所以我想检测到selectedSegmentedIndex的变化,然后设置颜色、背景颜色等,因为不能干扰注册UIControlStateValueDidChange

的事件,网上搜索一番没找到合适的方法,去检测selectedSegmentIndex的改变,突然想到KVO,这里简单的使用了KVO去检测

selectedSegmentIndex的初始化、改变的事件。


1.子类化UISegmentedControl

HJSegmentdControl.h

#import <UIKit/UIKit.h>@interface HJSegmentdControl : UISegmentedControl@end


HJSegmentdControl.m

//  WKSSegmentdControl.m//  Sword////  Created by Sword on 3/27/15.////#import "HJSegmentdControl.h"@implementation HJSegmentdControl/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.*/- (instancetype)initWithItems:(NSArray *)items{    self = [super initWithItems:items];    if (self) {        [self setup];            }    return self;}- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        [self setup];    }    return self;}- (void)awakeFromNib{    [super awakeFromNib];    [self setup];}- (void)setup{        [self addObserver:self forKeyPath:@"selectedSegmentIndex" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil];        [[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} forState:UIControlStateSelected];    [[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : RGBCOLOR(99, 141, 229)} forState:UIControlStateNormal];    [[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : RGBACOLOR(99, 141, 229, 0.7)} forState:UIControlStateHighlighted];}- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{    if ([@"selectedSegmentIndex" isEqualToString:keyPath]) {        ITTDINFO(@"selectedSegmentIndex %ld", self.selectedSegmentIndex);        NSArray *subViews = self.subviews;        id subView;        for (int i = 0; i < [subViews count]; i++) {            subView = [subViews objectAtIndex:i];            if ([subView respondsToSelector:@selector(isSelected)] && [subView isSelected]) {                [subView setTintColor:RGBCOLOR(99, 141, 229)];                [subView setBackgroundColor:RGBCOLOR(99, 141, 229)];            }            if ([subView respondsToSelector:@selector(isSelected)] && ![subView isSelected]) {                [subView setTintColor:[UIColor whiteColor]];                [subView setBackgroundColor:[UIColor whiteColor]];            }        }    }}@end

2.重写一些方法,在setup里设置样式,注册KVO事件

- (instancetype)initWithFrame:(CGRect)frame - (instancetype)initWithItems:(NSArray *)items- (void)awakeFromNib
3.在
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

里检测selectedSegmentIndex的变化,设置选中index的背景色,非选中的背景色,tintColor。

0 0
原创粉丝点击