Search code examples
iosxcodeuiviewkey-value-observing

Detect movement of UIView inside another UIView


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;

Solution

  • 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, MyView and InsideView classes 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 property positionInWindow and updated it whenever the superView moved.

    So in InsideView.m:

    // Make sure this property exists in .h file to make the class KVC compliant
    @synthesize positionInWindow;
    
    // This method is called when the super view changes.
    - (void)didMoveToSuperview
    {
        // Drop a random log message
        NSLog(@"MAI SUPERVIEW HAS CHANGED!!!");
    
        // Start observing superview's frame
        [self addObserver:self 
               forKeyPath:@"superview.frame" 
                  options: NSKeyValueObservingOptionNew 
                  context:nil];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath 
                          ofObject:(id)object 
                            change:(NSDictionary *)change 
                           context:(void *)context
    {
        // Here we update the positionInWindow because 
        // we know the superView.frame has changed
        CGPoint frameOrigin = self.frame.origin;
        [self setPositionInWindow:[[self window] convertPoint:frameOrigin
                                                     fromView:self]];
    }
    

    And anywhere you want to monitor this view:

    // On an instance of InsideView
    [insideView addObserver:self 
                 forKeyPath:@"positionInWindow" 
                    options:NSKeyValueObservingOptionNew 
                    context:nil];
    

    The good part about this solution is that InsideView does not need to know who is it's superView. This code will work even if the superView is later changed. There is no modification to the MyView class. And any class can monitor it's property independent of that fact.