KVO总结

//

//  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];

第一个参数,观察者对象 上述代码中的观察对象是自己self 必须实现observeValueForKeyPath:ofObject:change:context:代理

第二个参数 观察者对象中需要观察的成员变量 上述代码的观察变量是self里面的rect

第三个参数 options

NSKeyValueObservingOptionNew 把更改之前的值提供给处理方法

NSKeyValueObservingOptionOld 把更改之后的值提供给处理方法

NSKeyValueObservingOptionInitial 把初始化的值提供给处理方法,一旦注册,立马就会调用一次。通常它会带有新值,而不会带有旧值。

NSKeyValueObservingOptionPrior 分2次调用。在值改变之前和值改变之后。

第四个参数context

context: 可以带入一些参数,其实这个挺好用的,任何类型都可以,自己强转就好了。

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.实现观察者代理方法

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

4.最好不在需要观察该变量时,注销观察代理

[self removeObserver:self forKeyPath:@"rect"];

你可能感兴趣的:(KVO总结)