我有一个 UIView,我设置它来响应捏手势并改变它的大小,除了,一旦你放大它然后再次尝试捏它,它会跳到它的原始大小(恰好是 100x200) .这是代码:
@implementation ChartAxisView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// do gesture recognizers
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(onPinch:)];
[self addGestureRecognizer:pinch];
[pinch release];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0].CGColor;
CGContextSetFillColorWithColor(context, redColor);
CGContextFillRect(context, self.bounds);
}
- (void)onPinch: (UIPinchGestureRecognizer*) gesture {
self.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
}
- (void)dealloc {
[super dealloc];
}
@end
有什么想法吗?
最佳答案
所以有两种类型的缩放(或一般变换)函数:CGAffineTransformMakeScale和 CGAffineTransformScale
第一个,您正在使用的 CGAffineTransformMakeScale
,始终根据图像的原始大小进行变换。这就是为什么您会看到在缩放发生之前跳转到其原始大小的原因。
第二个,CGAffineTransformScale
,从图像的当前位置变换。这就是你所需要的。为此,它需要一个额外的“转换”参数。您案例中的“转换”arg 代表放大的图像。
阅读this关于转换的非常有用的博文。
关于objective-c - CGAffineTransformMakeScale 使 UIView 在缩放前跳转到原始大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3946797/