IBAction不适用于UITapGestureRecognizer

时间:2013-04-10 10:03:15

标签: objective-c ibaction uitapgesturerecognizer

我正在开发一个应用程序,我需要在点击UIView时缩放UITapGestureRecognizer,并希望在缩放和缩放后执行IBAction。这可能吗?。请给我一个小例子。

3 个答案:

答案 0 :(得分:1)

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
    tapGestureRecognizer.numberOfTapsRequired = 2;
    [self.scrollContainer addGestureRecognizer:tapGestureRecognizer];
}
- (void)handleDoubleTap:(UIGestureRecognizer *)recognizer
{
    if(isAlreadyZoomed)
    {
        CGPoint Pointview = [recognizer locationInView:recognizer.view];
        CGFloat newZoomscal = 3.0;
        CGSize scrollViewSize = self.scrollContainer.bounds.size;
        CGFloat width = scrollViewSize.width/newZoomscal;
        CGFloat height = scrollViewSize.height /newZoomscal;
        CGFloat xPos = Pointview.x-(width/2.0);
        CGFloat yPos = Pointview.y-(height/2.0);
        CGRect rectTozoom = CGRectMake(xPos, yPos, width, height);
        [self.scrollContainer zoomToRect:rectTozoom animated:YES];
        [self.scrollContainer setZoomScale:3.0 animated:YES];
        isAlreadyZoomed = NO;
    }
    else
    {
        [self.scrollContainer setZoomScale:1.0 animated:YES];
        isAlreadyZoomed = YES;
    }
}

答案 1 :(得分:0)

IBAction只是返回void的常规方法,只接受0或1参数。

您可以在代码中调用它们,就像调用任何其他方法之王一样。

UITapGestureRecognizer设计用于在触发时调用IBAction方法。您可以从InterfaceBuilder或代码

中进行设置

例如以下代码

 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:[tap cop]];
当用户doubleTap imageView

时,

将调用此方法

- (void) handleTap:(UITapGestureRecognizer *)sender;

在这种方法中,你几乎可以做任何你需要的事情: 管理您的子视图, 调用其他方法等... 但是,如果您计划放大和缩小,我强烈建议使用UIScrollView类而不是UIView类。

干杯

答案 2 :(得分:0)

我希望下面会给你一个想法......

UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
 tap2.numberOfTapsRequired = 2;
 [self.view addGestureRecognizer:tap2];


- (void) scrollViewDoubleTapped:(UITapGestureRecognizer *)sender
{
   scrollZoomAdjust.zoomScale=2.0f;// set your required Zoom scale 
   // scrollZoomAdjust is the scroll view that contain the image within it 

}

以上代码未经过测试