UIPinchGestureRecognizer在xcode 4.3.2中不起作用

时间:2012-04-27 13:14:43

标签: iphone xcode ios-simulator uipinchgesturerecognizer

使用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。

2 个答案:

答案 0 :(得分:1)

在.h文件中添加UIGestureRecognizerDelegate

在视图中的.m文件中使用以下代码加载...

// Gesture Reconizer Delegate Methods
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
 }

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  return  YES;
 }

 -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
   return  YES;
 }



UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(checkpinch)];
[pinch setDelegate:self];
[pinch setDelaysTouchesBegan:YES];
[self.ScrollView addGestureRecognizer:pinch];
[pinch release];

//手势重新调整方法

-(void)checkpinch {    

 NSLog(@"YES");
}

&gt;&gt;看到此编辑..

如果要在视图上检测到,则可以在滚动视图上检测到Pinch,而不是参见以下参考...

Detect pinch on UIView

希望,这会对你有帮助......

答案 1 :(得分:0)

也许我过于简单化了,但不容易:1)在你的VC中,为你的视图实例创建一个IBoutlet属性2)将IBOutlet连接到你的视图3)在VC中设置你的UIPinchGetsureRecognizer覆盖视图setter方法并将目标更改为视图并将pinch方法保留在视图中。这样,每次加载视图时都会加载UIPinchGetsureRecognizer并准备好进行压缩。那么你就不需要调用捏合方法(这会破坏目的)。 KB