Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:26:10+00:00 2026-06-10T23:26:10+00:00

What I have: When I move a UIView I am detecting its movement by

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:26:12+00:00Added an answer on June 10, 2026 at 11:26 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in a UIView subclass that will move it off
I have a UIView, it is not inside UIScrollView. I would like to move
I have some a UIView with several user created subviews. The user can move
I'd like to have a view (UIView) with two UIImageView subviews, and then move
I am trying to move a UIView programmatically using the following method: -(void)animatePlaybackLine: (int)
I have a small UIView and want to let it move to center of
I have a UIView, I am changing its center but the UIView isn't moving.
I have 6 UIView inside a ScrollView and with a panGestureRecognizer object I move
I have a UIView subclass called Card that I move around on my board
I have drawn a UIView and then few CALayers as sublayers on its root

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.