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)awakeFromNib3.在
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
里检测selectedSegmentIndex的变化,设置选中index的背景色,非选中的背景色,tintColor。
0 0
- ios UISegmentedControl KVO 检测selectedSegmentIndex的改变
- iOS UISegmentedControl改变样式
- ios UISegmentedControl的使用
- IOS的UISegmentedControl用法
- iOS UISegmentedControl 的简单使用
- iOS--UISegmentedControl的使用方法详细
- andorid模仿iOS的UISegmentedControl
- selectedSegmentIndex显示为-1的小问题
- IOS UISegmentedControl
- IOS UISegmentedControl
- ios kvo 的用法
- IOS 的 KVO机制
- UISegmentedControl 改变选中颜色
- iOS—UISegmentedControl的详细使用
- UISegmentedControl的使用 IOS学习手记
- iOS——UISegmentedControl 的用法
- 我的iOS学习历程 - UISegmentedControl
- iOS——UISegmentedControl的使用
- jQuery ui 拖放函数
- 高效率的取幂运算
- 6个APP应用市场运营推广方法
- P51 第二章 第9题
- Python sys.path详细介绍
- ios UISegmentedControl KVO 检测selectedSegmentIndex的改变
- JAVA版配置QPID
- IOS JSON解析之JSONKit使用
- 线程池代替进程池
- 在ubuntu下对多个C++文件进行同时编译的例子
- 系统自动化配置和管理工具:SaltStack
- 查找学生信息(map使用)
- python操作Excel读写--使用xlrd
- Android开发_简单的网络编程