0

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

4

2 回答 2

1

添加UIGestureRecognizerDelegate.h文件

在 .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");
}

>>看这个编辑..

如果您想在视图上检测而不是查看以下参考,则捏可以在滚动视图上检测...

检测 UIView 上的捏合

希望对你有帮助...

于 2012-04-27T13:20:10.697 回答
0

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

于 2012-04-27T14:16:06.670 回答