使用 xCode 4.3.2,我在 view.m 的 initWithFrame 中有以下代码:
UIPinchGestureRecognizer *recognizer = [[UIPinchGestureRecognizer alloc]
initWithTarget: self action: @selector(pinch:)
];
oldScale = recognizer.scale;
[self pinch: recognizer];
[self addGestureRecognizer: recognizer];
// code to create label is also in here, works. label displays no problem
[self addSubview: label];
我的捏法如下:
- (void) pinch: (UIPinchGestureRecognizer *) recognizer
{
NSLog(@"pinch:");
label.bounds = CGRectMake(
(self.bounds.size.width - width * recognizer.scale) / 2,
(self.bounds.size.height - height * recognizer.scale) / 2,
width * recognizer.scale,
height * recognizer.scale
);
label.font = [UIFont systemFontOfSize: 20 * recognizer.scale];
NSString *verdict;
if (recognizer.scale > oldScale) {
verdict = @"spread";
} else if (recognizer.scale < oldScale) {
verdict = @"pinch";
} else {
verdict = @"neither";
}
oldScale = recognizer.scale;
label.text = [NSString stringWithFormat: @"%@ %g",
verdict, recognizer.scale
];
}
因为我实际上在我的 initWithFrame 方法中调用了 pinch,它运行一次,但是当我在 iphone 模拟器中执行 pinch 时,它根本没有注册。xcode 4.3.2 中有一些我不知道的设置吗?这段代码在我尝试运行它的其他任何地方都有效——但这些版本的 xcode 都是 4.3。