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 6547857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:52:55+00:00 2026-05-25T11:52:55+00:00

I want to have a movable/scalable/rotable view inside another view. The inner view can

  • 0

I want to have a movable/scalable/rotable view inside another view. The inner view can go outside the outer view’s frame but I want to keep part of it inside the outer one so the inner view isn’t lost.

I simplified the problem in this xcode project https://github.com/nextorlg/Intersection.

If the inner view was only movable and scalable the problem would be already solved but when the inner view rotates this solution it is not good because the frame contains the view but it is not the view itself.

Every transformation performed to the inner view I use this function to validate the new view position, if it is not valid I revert the last transformation ( this is the movable view code https://github.com/nextorlg/Intersection/blob/master/intersec/MovableView.m )

-(BOOL) validInset {
    CGRect outerLimit = CGRectMake(0, 0, self.superview.frame.size.width, self.superview.frame.size.height);
    CGRect intersectionRect = CGRectIntersection(self.frame, outerLimit);
    NSLog(@"self.frame:%f,%f,%f,%f", self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
    NSLog(@"outer.frame:%f,%f,%f,%f", outerLimit.origin.x, outerLimit.origin.y, outerLimit.size.width, outerLimit.size.height);
    NSLog(@"intersec.frame:%f,%f,%f,%f", intersectionRect.origin.x, intersectionRect.origin.y, intersectionRect.size.width, intersectionRect.size.height);
    NSLog(@"========================");
    if ( CGRectIsNull(intersectionRect) ||
         intersectionRect.size.width < INSET ||
         intersectionRect.size.height < INSET ) {
         return NO;
    }
    else {
        return YES;
    }
}

The question is, how can I be sure the inner view it is not lost behind the outer view when the first is rotated some (45 for example) degrees and dragged to a corner?

One comment, I want to keep just some pixels inside the outer view because the inner view can be bigger (scaled) than the outer one.

I recommend you to download and run the project to understan better the problem, it is difficult to understand it just reading this.

Thank you!

  • 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-05-25T11:52:56+00:00Added an answer on May 25, 2026 at 11:52 am

    As you stated, your method will only work fine when the inner view is not rotated.

    The easiest method I am aware of for solving your problem is simply to test if the center or a vertex of one of the views is inside the other one.

    -(BOOL) validInset {
        // Get the vertices of A (outerLimit)
        CGRect outerLimit = self.superview.bounds;
        CGPoint pointA[] = {
            CGPointMake(outerLimit.origin.x,   outerLimit.origin.y    ),
            CGPointMake(outerLimit.size.width, outerLimit.origin.y    ),
            CGPointMake(outerLimit.size.width, outerLimit.size.height ),
            CGPointMake(outerLimit.origin.x,   outerLimit.size.height )};
        // Adjust outerLimit's borders
        pointA[0].x += INSET, pointA[0].y += INSET;
        pointA[1].x -= INSET, pointA[1].y += INSET;
        pointA[2].x -= INSET, pointA[2].y -= INSET;
        pointA[3].x += INSET, pointA[3].y -= INSET;
        // Get the vertices of B (innerView)
        CGRect innerView = self.bounds;
        CGPoint pointB[] = {
            CGPointMake(innerView.origin.x,   innerView.origin.y    ),
            CGPointMake(innerView.size.width, innerView.origin.y    ),
            CGPointMake(innerView.size.width, innerView.size.height ),
            CGPointMake(innerView.origin.x,   innerView.size.height )};
        // Test if the center of B is inside A or vice versa
        CGPoint center, converted;
        center = CGPointMake(pointB[0].x + (pointB[1].x - pointB[0].x)/2, pointB[0].y + (pointB[2].y - pointB[0].y)/2);
        if( converted = [self convertPoint:center toView:self.superview],
            converted.x >= pointA[0].x && 
            converted.x <= pointA[1].x &&
            converted.y >= pointA[0].y &&
            converted.y <= pointA[2].y ) return YES;
        center = CGPointMake(pointA[0].x + (pointA[1].x - pointA[0].x)/2, pointA[0].y + (pointA[2].y - pointA[0].y)/2);
        if( converted = [self convertPoint:center toView:self.superview],
            converted.x >= pointA[0].x && 
            converted.x <= pointA[1].x &&
            converted.y >= pointA[0].y &&
            converted.y <= pointA[2].y ) return YES;
        // Test if vertices of B are inside A or vice versa
        for (int i = 0; i < 4; i++) {
            if( converted = [self convertPoint:pointB[i] toView:self.superview],
                converted.x >= pointA[0].x && 
                converted.x <= pointA[1].x &&
                converted.y >= pointA[0].y &&
                converted.y <= pointA[2].y ) return YES;
            if( converted = [self.superview convertPoint:pointA[i] toView:self],
                converted.x >= pointB[0].x && 
                converted.x <= pointB[1].x &&
                converted.y >= pointB[0].y &&
                converted.y <= pointB[2].y ) return YES;
        }
        return NO;
    }
    

    This code will do the job for dealing with the transformations that you use in your example, but it is not a general purpose answer.

    A non iphone-related answer to this question is found here.

    And you can also read some background about rectangle and point intersection here.

    edit:

    For the transformations that can be applied to an UIView, to test the vertices and the center will be enough for most cases.

    The two well-known exceptions are:

    • when h1 < h2/2 and w1/2 > w2 (or h2 < h1/2 and w2/2 > w1),
    • when 2w2 < h2/2 and r1 > w2 (or 2w1 < h1/2 and r2 > w1);

    where:

    • r = the distance between the center of the UIView and a vertex;
    • w = the length of the shortest side of the UIView;
    • h = the length of the longest side of the UIView.

    If you face one of these cases, you should use another method, like the one in the first link.

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

Sidebar

Related Questions

want to have a Hyperlink-Button in a gridView in which I can display a
I want to have a text box that the user can type in that
I want to have a PHP script send a XML formatted string to another
I have a method in Test1Activity that creates tabs inside a TabHost, but if
I want have an insert query, but before inserting I check whether the username
I want have a view like iPhone's calendars app. Anybody help to show me
Basically, I want have two separate SQL queries, but I want them to be
I want to have something like a big still image that you can move
Suppose you want to take advantage of move semantics, but one of your movable
I want have a table in my view that is going to put 3

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.