What I have:
When I move a UIView I am detecting its movement by doing the following:
[myView.layer addObserver:self forKeyPath:@"position" options:NSKeyValueObservingOptionNew context:nil];
Being myView the UIView I am moving and self a class I have to detect the different positions the UIView has.
The Issue:
When I put myView inside another UIView and I move the anotherView:
[anotherView addSubview: myView];
The method:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
Is not called anymore, although in theory myView is moving as well. I tried to use NSNotification to be fired every time a “movement” occurred, but I find it clumsy. Is there an “elegant” solution for this kind of problem?
For the movement of the UIView I am using this methods:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
I have uploaded a sample project here that does this cleanly:
https://github.com/n9986/ObservingUIViewMovement
I had a similar requirement sometime back. The issue here is that when you add the UIView to another UIView (call it superView), it now resides in the coordinate space of superView. Therefore, the movement of superView in it’s parent’s coordinate space does not effect it’s children.
I will explain the code here a bit.
We have
ViewController,MyViewandInsideViewclasses illustrating your typical classes. I want to observe in the viewController whether the InsideView has moved or not. So in the custom class, I added a propertypositionInWindowand updated it whenever the superView moved.So in
InsideView.m:And anywhere you want to monitor this view:
The good part about this solution is that
InsideViewdoes not need to know who is it’ssuperView. This code will work even if thesuperViewis later changed. There is no modification to theMyViewclass. And any class can monitor it’s property independent of that fact.