//
//? CBRectPositionController.m
//? KVOPractice
//
//? Created by jimzhai on 6/14/14.
//? Copyright (c) 2014 zhaishuai. All rights reserved.
//
#import "CBRectPositionController.h"
@interface CBRectPositionController()
@property (nonatomic)double xPosition;
@property (nonatomic)double yPosition;
@property (nonatomic)double width;
@property (nonatomic)double height;
@property (nonatomic)CGRect rect;
@property (weak, nonatomic) IBOutlet UILabel *colorLabel;
@end
@implementation CBRectPositionController
-(void) dealloc{
[self removeObserver:self forKeyPath:@"rect"];
}
- (IBAction)xPositionChanged:(UISlider *)sender {
self.xPosition = sender.value;
}
- (IBAction)yPositionChanged:(UISlider *)sender {
self.yPosition = sender.value;
}
- (IBAction)widthChanged:(UISlider *)sender {
self.width = sender.value;
}
- (IBAction)heightChanged:(UISlider *)sender {
self.height = sender.value;
}
- (CGRect)rect{
return CGRectMake(self.xPosition*100,
self.yPosition*100,
self.width*200,
self.height*200);
}
- (void)viewDidLoad{
[super viewDidLoad];
NSLog(@"addObserver");
[self addObserver:self forKeyPath:@"rect" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil];
[self.colorLabel setBackgroundColor:self.colorLabelBackgroundColor];
self.xPosition = 0.5;
self.yPosition = 0.5;
self.width = 0.5;
self.height = 0.5;
[self.colorLabel setFrame:CGRectMake(self.xPosition*100, self.yPosition*100, self.width*200, self.height*200)];
}
//+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
//{
//? ? NSSet* keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
//
//? ? if([key isEqualToString:@"rect"])
//? ? {
////? ? ? ? keyPaths = [NSSet setWithObjects:@"xPosition",@"yPosition",@"width",@"height", nil];
//? ? ? ? keyPaths = [NSSet setWithObjects:@"xPosition",nil];
//? ? }
//
//? ? return keyPaths;
//}
//+ (NSSet *)keyPathsForValuesAffectingRect{
//? ? return [NSSet setWithObjects:@"xPosition",@"yPosition",@"width",@"height", nil];
//}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if([keyPath isEqualToString:@"rect"]){
NSLog(@"changed");
[self.colorLabel setFrame:self.rect];
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
@end
1.首先在代碼中添加觀察者
[self addObserver:self forKeyPath:@"rect" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil];
第一個參數(shù),觀察者對象 上述代碼中的觀察對象是自己self 必須實現(xiàn)observeValueForKeyPath:ofObject:change:context:代理
第二個參數(shù) 觀察者對象中需要觀察的成員變量 上述代碼的觀察變量是self里面的rect
第三個參數(shù) options
NSKeyValueObservingOptionNew 把更改之前的值提供給處理方法
NSKeyValueObservingOptionOld 把更改之后的值提供給處理方法
NSKeyValueObservingOptionInitial 把初始化的值提供給處理方法队橙,一旦注冊舌狗,立馬就會調(diào)用一次接奈。通常它會帶有新值奈懒,而不會帶有舊值。
NSKeyValueObservingOptionPrior 分2次調(diào)用娃殖。在值改變之前和值改變之后毅舆。
第四個參數(shù)context
context: 可以帶入一些參數(shù)西篓,其實這個挺好用的,任何類型都可以憋活,自己強轉(zhuǎn)就好了岂津。
2.設置可能會影響觀察變量值的變量
例如在上面的代碼中,沒有直接修改觀察變量rect悦即,但是修改"xPosition", "yPosition","width","height"都會修改rect的值吮成,所以可以使用下面的兩個方式增加可能會影響rect值的變量,一旦"xPosition", "yPosition","width","height"被修改都會掉用觀察者代理方法辜梳,建議使用第二種方式
方式一:
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
{
NSSet* keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
if([key isEqualToString:@"rect"])
{
//? ? ? ? keyPaths = [NSSet setWithObjects:@"xPosition",@"yPosition",@"width",@"height", nil];
keyPaths = [NSSet setWithObjects:@"xPosition",nil];
}
return keyPaths;
}
方式二:
+ (NSSet *)keyPathsForValuesAffectingRect{
return [NSSet setWithObjects:@"xPosition",@"yPosition",@"width",@"height", nil];
}
3.實現(xiàn)觀察者代理方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
4.最好不在需要觀察該變量時粱甫,注銷觀察代理
[self removeObserver:self forKeyPath:@"rect"];