开发者

Prevent subviews from resizing when setting view's transform with CGAffineTransformScale()

开发者 https://www.devze.com 2023-02-11 02:46 出处:网络
If I d开发者_运维问答o : view.transform = CGAffineTransformScale(view.transform, s, s); My view is scaled, but also its subviews... I don\'t want the subview to change size. Apparently autoresizesS

If I d开发者_运维问答o :

view.transform = CGAffineTransformScale(view.transform, s, s);

My view is scaled, but also its subviews... I don't want the subview to change size. Apparently autoresizesSubviews has no effect on this matter


Suppose that your UIView's stack is arranged as follow:

@interface UIResizableView : UIView <UIGestureRecognizerDelegate> {
    UILabel *innerView;
    CGPoint startPoint;
}
@end

@implementation UIResizableView
- (id)initWithFrame:(CGRect)aFrame {
    self = [super initWithFrame:aFrame];
    if (self) {
    innerView = [[UILabel alloc] initWithFrame: CGRectMake(10, 10, 115, 50)];
    [innerView setFont:[UIFont systemFontWithSize:7]];
    [innerView setTextColor:[UIColor blackColor]];
    [innerView setBackgroundColor:[UIColor clearColor]];
    [innerView setTextAlignment:UITextAlignmentCenter];
    [innerView setText:@"hello world!"];
    [self innerView];
    [innerView release];

    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
    [pinchRecognizer setDelegate:self];
    [self addGestureRecognizer:pinchRecognizer];
    [pinchRecognizer release];
    }
    return self;
}
@end

Then your scale method would look like the following. Note that the "reframing" of innerView is required to center again the subview in the original position.

-(void)scale:(id)sender {
    static float lastScale = 0.0;
    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
        lastScale = 1.0;
        return;
    }
    CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

    CGAffineTransform currentTransform = self.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    [self setTransform:newTransform];

    CGAffineTransform originalSize = CGAffineTransformInvert(self.transform);
    CGAffineTransform originalPositionAndSize = CGAffineTransformTranslate(originalSize, self.frame.origin.x, self.frame.origin.y);
    [innerView setTransform:originalPositionAndSize];

    innerView = CGRectMake(10, 10, 115, 60);

    lastScale = [(UIPinchGestureRecognizer*)sender scale];
}


Perhaps you are adding the subview (and using the parent view's frame values to set the size) after you have applied the transform on the parent view. At least that was what I was doing.

I had to compensate for that by applying a inversion

CGRect parentOriginalFrame = CGRectApplyAffineTransform(parentView.frame, CGAffineTransformInvert(parentView.transform));

Now with the parent's original frame, you can set the right frame value for the child.

0

精彩评论

暂无评论...
验证码 换一张
取 消