gpt4 book ai didi

ios - 使用 CGAffineTransformScale() 设置 View 的变换时防止 subview 调整大小

转载 作者:可可西里 更新时间:2023-11-01 04:49:05 24 4
gpt4 key购买 nike

如果我这样做:

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

我的 View 已缩放,但它的 subview 也已缩放...我不希望 subview 更改大小。显然 autoresizesSubviews 对此事没有影响

最佳答案

假设您的 UIView 的堆栈排列如下:

@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

然后您的缩放方法将如下所示。请注意,innerView 的“重构”需要在原始位置再次居中 subview 。

-(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];
}

关于ios - 使用 CGAffineTransformScale() 设置 View 的变换时防止 subview 调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5004118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com