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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,144评论 1 6
  • 概念 先来看看概念,Key-value coding (KVC) 和 key-value observing (K...
    wuwy阅读 1,400评论 0 1
  • 1、禁止手机睡眠 [UIApplication sharedApplication].idleTimerDisab...
    FF_911阅读 1,431评论 0 3
  • 最近一段时间一直在思考一个问题:我究竟想成为一个什么样的人,想过什么的生活,我的职业生涯规划该是如何?说实话,思考...
    斜杠时光阅读 271评论 0 0
  • 提筆卻不知如何寫起,思緒亂,夾雜著胃痛, 而疼痛的感觸不如心情的傷痛 她生氣,我胃痛,卻不如朋友的一杯溫水 我惹她...
    言博阅读 224评论 0 1