objective-c - CGAffineTransformMakeScale 使 UIView 在缩放前跳转到原始大小

标签 objective-c cocoa-touch ipad ios4

我有一个 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

有什么想法吗?

最佳答案

所以有两种类型的缩放(或一般变换)函数:CGAffineTransformMakeScaleCGAffineTransformScale

第一个,您正在使用的 CGAffineTransformMakeScale,始终根据图像的原始大小进行变换。这就是为什么您会看到在缩放发生之前跳转到其原始大小的原因。

第二个,CGAffineTransformScale,从图像的当前位置变换。这就是你所需要的。为此,它需要一个额外的“转换”参数。您案例中的“转换”arg 代表放大的图像。

阅读this关于转换的非常有用的博文。

关于objective-c - CGAffineTransformMakeScale 使 UIView 在缩放前跳转到原始大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3946797/

相关文章:

ios - 连接的蓝牙模块未显示在 EAAccessoryManager 的连接附件中

Objective-C – cellForRowAtIndexPath 导致按钮框架改变大小?

objective-c - objective-c/cocoa-touch 中有哪些对数函数/方法?

iphone - 在应用程序启动之间保留数据

iphone - 如何使用CGContext iOS在其他设备方向绘制字符串

asp.net - 如何在 IIS 中启用字节范围的 http 请求

objective-c - iOS:适用于 iPad 的 MFMessageComposeViewController

ios - 使用文件提供程序实现 UIDocumentPickerModeOpen

objective-c - iOS 应用程序更新,如果用户跳过更新会发生什么

objective-c - 重写 NSArray 文字以在 OS X 10.6 的 gcc 上编译