UIPinchGestureRecognizer无法在Xcode / iPhone模拟器中运行

时间:2012-12-22 05:07:54

标签: iphone xcode simulator

在Xcode / iPhone模拟器中工作,我有一个应用程序,其中UIGestureRecognizers正在进行捏,点击和平移。在不更改任何代码的情况下,不再识别捏(我在移动鼠标时按住选项键以显示两个灰色圆圈一起移动或分开)。平移和点击仍然都有效。还有其他人遇到过这个问题吗?

识别器本身或模拟器似乎出现了问题,因为下面的代码永远不会被调用,但它适用于平移。

- (void)pinch:(UIPinchGestureRecognizer *)gesture
{
NSLog(@"in pinch method");
if ((gesture.state == UIGestureRecognizerStateChanged) ||
    (gesture.state == UIGestureRecognizerStateEnded)) {
    self.scale *= gesture.scale; // adjust our scale
    gesture.scale = 1;           // reset gestures scale so future changes are incremental
}
}

- (void)tap:(UITapGestureRecognizer *)gesture
{
NSLog(@"in tap method");
if (gesture.state == UIGestureRecognizerStateEnded) 
    self.originInPixels = [gesture locationInView:self];
}

我尝试使用简单的MKMapView创建一个新的应用程序,它可以正确加载地图,平移和点击工作 - 但是仍然无法正常工作。

我在iOS 5.1中工作。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

请试试这个

将此代码包含在您的项目中

example.h文件

CGFloat     lastScale;
CGPoint     lastPoint;

example.m

- (无效)viewDidLoad中 {

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
    [self addGestureRecognizer:pinchGesture];
    [pinchGesture release];

}

- (void)handlePinch:(UIPinchGestureRecognizer *)gestureRecognizer {

if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
    // Reset the last scale, necessary if there are multiple objects with different scales
    lastScale = [gestureRecognizer scale];
}

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
    [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

    CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];
    NSLog(@"%f",currentScale);
    // Constants to adjust the max/min values of zoom
    const CGFloat kMaxScale = 3.0;
    const CGFloat kMinScale = 1.0;

    CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]);

    newScale = MIN(newScale, kMaxScale / currentScale);
    newScale = MAX(newScale, kMinScale / currentScale);
    CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
    NSLog(@"%f",lastScale);
    [gestureRecognizer view].transform = transform;

    lastScale = [gestureRecognizer scale];
}

}

答案 1 :(得分:1)

UIGestureRecognizer开始独立工作,我仍然不确定原因。它可能就像我没有按住鼠标按钮同时按住选项键一样简单,但我以为我之前尝试过。

感谢上面的建议。

答案 2 :(得分:0)

我的Macbook Air触控板上遇到了同样的问题,XCode 4.5.2认为[选项] + [单指触摸移动]会捏(灰球移入/移出)。

但是,您需要应用[选项] + [3指触摸]才能正常工作。

这可以在模拟器中运行,但在连接的iOS设备上运行时烦人无益。虽然我下载的应用程序可以运行,但是连接到XCode的iPhone运行时没有,唉!