================
手残把 @wokenshin 兄弟的回复给删除了… 不知道怎么撤销,
// 这里补充下, @wokenshin兄弟用Masonry修改约束后, 发现不能触发监听, 我这里也同样是该问题, 这里通过约束后再次设置_t.frame的方式, 可以出发该监听, 个人猜测监听动作是在set frame的时候才会触发. 而Masonry仅仅是修改了约束,layout...
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the
_t = [[TestView alloc] initWithNone];
_t.frame = CGRectMake(10, 10, 100, 100);
_t.backgroundColor = [[UIColor alloc] initWithRed:210/255 green:210/255 blue:21/255 alpha:1];
[self.view addSubview:_t];
}
-(void)viewDidAppear:(BOOL)animated{
NSLog(@"==> %f",_t.frame.origin.x);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// _t.frame = CGRectMake(100, 10, 90, 100);
[_t mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).with.offset(99);
make.top.equalTo(self.view).with.offset(111);
make.center.equalTo(self.view).with.centerOffset(CGPointMake(10, 10));
}];
[self.view layoutIfNeeded];
_t.frame = CGRectMake(_t.frame.origin.x, _t.frame.origin.y, _t.frame.size.width, _t.frame.size.height);
NSLog(@"==> %f",_t.frame.origin.x);
}
================
//记录一下
##通过添加KVO 对self:UIView 的frame属性进行监控,一旦frame变换 则发送通知调用需要的方法
- (instancetype)initWithConfig:(SDPopViewConfig *)config
{
self = [super init];
if (self) {
// kvo 监控自己的 frame 属性变化
[self addObserver:self forKeyPath:@"frame" options:0 context:NULL];
_config = config;
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:@"frame"]) {
CGRect newFrame = CGRectNull;
if([object valueForKeyPath:keyPath] != [NSNull null]) {
//此处为获取新的frame
newFrame = [[object valueForKeyPath:keyPath] CGRectValue];
//此处为调用的方法
[self setLayerFrame:newFrame];
}
}
}