在UIPopoverController中失去手势识别器

7
我有一个名为PhotoBoothController的类,用于使用手势操作图像。当我在iPhone上运行它时,它可以正常工作。但是,当我在iPad上运行它时,我会将PhotoBoothController显示在UIPopoverController中。图像显示正常,但是我的手势似乎无法被识别。我不知道如何让UIPopoverController控制手势。
我展示了popovercontroller并配置了视图:
- (void)presentPhotoBoothForPhoto:(UIImage *)photo button:(UIButton *)button {

    //Create a photoBooth and set its contents
    PhotoBoothController *photoBoothController = [[PhotoBoothController alloc] init];
    photoBoothController.photoImage.image = [button backgroundImageForState:UIControlStateNormal];

    //set up all the elements programmatically.
    photoBoothController.view.backgroundColor = [UIColor whiteColor];

    //Add frame (static)
    UIImage *frame = [[UIImage imageNamed:@"HumptyLine1Frame.png"] adjustForResolution];
    UIImageView *frameView = [[UIImageView alloc] initWithImage:frame];
    frameView.frame = CGRectMake(50, 50, frame.size.width, frame.size.height);
    [photoBoothController.view addSubview:frameView];

    //Configure image
    UIImageView *photoView = [[UIImageView alloc] initWithImage:photo];
    photoView.frame = CGRectMake(50, 50, photo.size.width, photo.size.height);
    photoBoothController.photoImage = photoView;

    //Add canvas
    UIView *canvas = [[UIView alloc] initWithFrame:frameView.frame];
    photoBoothController.canvas = canvas;
    [canvas addSubview:photoView];
    [canvas becomeFirstResponder];

    [photoBoothController.view addSubview:canvas];
    [photoBoothController.view bringSubviewToFront:frameView];

    //resize the popover view shown in the current view to the view's size
    photoBoothController.contentSizeForViewInPopover = CGSizeMake(frameView.frame.size.width+100, frameView.frame.size.height+400);

    self.photoBooth = [[UIPopoverController alloc] initWithContentViewController:photoBoothController];
    [self.photoBooth presentPopoverFromRect:button.frame
                                     inView:self.view
                   permittedArrowDirections:UIPopoverArrowDirectionAny
                                   animated:YES];
}

我认为[canvas becomeFirstResponder]可能可以实现,但似乎没有任何区别。如有建议,非常感谢,谢谢。
更新:根据评论添加代码。
- (void)viewDidLoad {
  [super viewDidLoad];

  if (!_marque) {
    _marque = [CAShapeLayer layer];
    _marque.fillColor = [[UIColor clearColor] CGColor];
    _marque.strokeColor = [[UIColor grayColor] CGColor];
    _marque.lineWidth = 1.0f;
    _marque.lineJoin = kCALineJoinRound;
    _marque.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];
    _marque.bounds = CGRectMake(photoImage.frame.origin.x, photoImage.frame.origin.y, 0, 0);
    _marque.position = CGPointMake(photoImage.frame.origin.x + canvas.frame.origin.x, photoImage.frame.origin.y + canvas.frame.origin.y);
  }
  [[self.view layer] addSublayer:_marque];

  UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
  [pinchRecognizer setDelegate:self];
  [self.view addGestureRecognizer:pinchRecognizer];

  UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
  [rotationRecognizer setDelegate:self];
  [self.view addGestureRecognizer:rotationRecognizer];

  UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
  [panRecognizer setMinimumNumberOfTouches:1];
  [panRecognizer setMaximumNumberOfTouches:1];
  [panRecognizer setDelegate:self];
  [canvas addGestureRecognizer:panRecognizer];

  UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
  [tapProfileImageRecognizer setNumberOfTapsRequired:1];
  [tapProfileImageRecognizer setDelegate:self];
  [canvas addGestureRecognizer:tapProfileImageRecognizer];

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && ![gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]];
}

请添加用于将手势识别器附加到您的UIPopoverController的代码。 - Stavash
@stavash 已完成 - 代码在 PhotoBoothController 类中。谢谢! - Smikey
这是一个有趣的问题。将手势外部添加到UIPopoverController中怎么样?我知道这需要做出相当多的更改,但我现在没有其他建议... - Stavash
2个回答

3

我正在开发一个类似的应用程序,可以轻松地在画布上操作图像。

1) 检查画布的 userInteractionEnabled 属性是否设置为 YES
2) 你可以尝试将手势添加到 ImageView 上而不是画布上。(我猜你想缩放、旋转和滑动图片)。


1

一些猜测:

  1. 这是因为在设置 cavas 属性之前已经调用了 viewDidLoad 方法吗?在这种情况下,您可以尝试在另一个方法中(例如 viewDidAppear)呈现弹出窗口后添加识别器。

  2. 我也同意 UIPopoverController 允许与弹出窗口外的其他视图进行交互的关键可能是。因此,我建议将 [canvas becomeFirstResponder]; 移动到 PhotoBoothController 中的 viewDidLoad 方法的末尾,并在此处尝试 self.view becomeFirstResponder,因为您实际上是将识别器添加到 self.view。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接