05056简单kvo

    //kvo0506

//    [_collectionView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];

 //kvo0506

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{

//    //keypath 是观察的属性

//    NSLog(@"---------keypath:%@",keyPath);

//    //被观察的对象

//    NSLog(@"---------object:%@",object);

//    //新旧值

    NSLog(@"---------change:%@",change);//change是字典

//

//    NSLog(@"---------context:%@",context);

    

    if ([change[NSKeyValueChangeNotificationIsPriorKey]boolValue]) {

        NSLog(@"1");

        NSLog(@"值改变之前");

    } else{

//        NSLog(@"2");

//        NSLog(@"值改变之后");

    }

    

    

    

    //    self.view.backgroundColor = [UIColor greenColor];

    

    //context的作用就是传值/////////////////////////////

//    ViewController *vc = (__bridge ViewController *)context;

//    

//    vc.view.backgroundColor = [UIColor greenColor];

 

 

 

}

 

-(void)dealloc{

    

    [_collectionView removeObserver:self forKeyPath:@"frame"];

}

原文地址:https://www.cnblogs.com/gzz2016/p/5465745.html