【发布时间】:2012-09-05 14:48:03
【问题描述】:
我有什么:
当我移动 UIView 时,我通过执行以下操作来检测它的移动:
[myView.layer addObserver:self forKeyPath:@"position" options:NSKeyValueObservingOptionNew context:nil];
作为myView 和UIView 我正在移动和self 一类我必须检测UIView 的不同位置。
问题:
当我将myView 放入另一个UIView 并移动anotherView:
[anotherView addSubview: myView];
方法:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
不再调用,尽管理论上myView 也在移动。每次发生“运动”时,我都尝试使用NSNotification 被解雇,但我觉得它很笨拙。这类问题有“优雅”的解决方案吗?
对于UIView的移动我用的是这个方法:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
【问题讨论】:
-
下面发布的解决方案对您有用吗?
标签: ios xcode uiview key-value-observing