简体   繁体   中英

How to get frame of subview after apply transform on mainView?

I have created mainView objcet of UIView and added one subview on it. I applied transform on mainView for reducing frame size. But frame of subview of mainView was not reduced. How to reduce the size of this subview.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    CGFloat widthM=1200.0;
    CGFloat heightM=1800.0;
    UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, widthM, heightM)];
    mainView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"te.png"]];
    [self.view addSubview:mainView];
    CGFloat yourDesiredWidth = 250.0;
    CGFloat yourDesiredHeight = yourDesiredWidth *heightM/widthM;
    CGAffineTransform scalingTransform;
    scalingTransform = CGAffineTransformMakeScale(yourDesiredWidth/mainView.frame.size.width, yourDesiredHeight/mainView.frame.size.height);
     mainView.transform = scalingTransform;
    mainView.center = self.view.center;
    NSLog(@"mainView:%@",mainView);
    UIView *subMainView= [[UIView alloc] initWithFrame:CGRectMake(100, 100, 1000, 1200)];
    subMainView.backgroundColor = [UIColor redColor];
    [mainView addSubview:subMainView];
    NSLog(@"subMainView:%@",subMainView);

}

NSlog of these views:

mainView:<UIView: 0x8878490; frame = (35 62.5; 250 375); transform = [0.208333, 0, 0, 0.208333, 0, 0]; layer = <CALayer: 0x8879140>>
subMainView:<UIView: 0x887b8c0; frame = (100 100; 1000 1200); layer = <CALayer: 0x887c160>>

Here the width of mainView is 250, the width of subview is 1000. but when i get the output in simulator, subview is occupied correctly, but it's not cross the mainView. How it is possible? How to get frame of subview with respect mainView frame after transformation?

What you're seeing is expected behavior. The frame of an UIView is relative to its parent, so it doesn't change when you apply a transformation to its superview. While the view will appear 'distorted' too, the frame won't reflect the changes since it's still at exact the same position relative to its parent.
However, I assume you would like to get the frame of the view relative to the topmost UIView . In that case UIKit offers these functions:

  • – [UIView convertPoint:toView:]
  • – [UIView convertPoint:fromView:]
  • – [UIView convertRect:toView:]
  • – [UIView convertRect:fromView:]

I applied these to your example:

CGRect frame = [[self view] convertRect:[subMainView frame] fromView:mainView];
NSLog(@"subMainView:%@", NSStringFromCGRect(frame));

And this is the output:

subMainView:{{55.8333, 83.3333}, {208.333, 250}}

In addition to s1m0n answer, the beautiful thing about applying a transform matrix to your view, is that you can keep reasoning in terms of its original coordinate system (in your case, you can handle subMainView using the non-transformed coordinate system, which is why, even though subMainView's frame is bigger than mainView's transformed frame, it still doesn't cross the parent view, as it gets automatically transformed). This means that when you have a transformed parent view (for example rotated and scaled) and you want to add a subview in a particular point relative to this parent view, you don't have to first keep track of the previous transformations in order to do so.

If you really are interested in knowing the subview's frame in terms of the transformed coordinate system, it will be enough to apply the same transformation to the subview's rectangle with:

CGRect transformedFrame = CGRectApplyAffineTransform(subMainView.frame, mainView.transform);

If you then NSLog this CGRect , you will obtain:

Transformed frame: {{20.8333, 20.8333}, {208.333, 250}}

Which, I believe, are the values that you were looking for. I hope this answers your question!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM