7

我有一個名爲PhotoBoothController的類,用於使用手勢操縱圖像。當我在iPhone上運行它時,它工作正常。但是,當我在iPad上運行它時,我會在UIPopoverController中顯示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]可能做到這一點,但它似乎不有所作爲。

任何建議非常感謝,謝謝。

UPDATE:添加代碼,根據註釋

- (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]]; 
} 
+0

請將用於附加手勢識別器的代碼添加到您的UIPopoverController – Stavash

+0

@stavash done - 代碼位於PhotoBoothController類中。謝謝! – Smikey

+0

這是一個有趣的問題。如何將手勢外部添加到UIPopoverController?我知道這需要做很多改動,但我現在沒有其他建議。 – Stavash

回答

1

一些猜測:

  1. 這是因爲之前已設置卡瓦酒財產viewDidLoad方法已經叫什麼名字?在這種情況下,您可以嘗試使用另一種方法(如viewDidAppear)在彈出窗口中添加識別器。

  2. 我也同意,關鍵可能是UIPopoverController允許與popover之外的其他視圖進行交互。所以我建議將[canvas becomeFirstResponder];移動到PhotoBoothControllerviewDidLoad方法的末尾,並在此嘗試self.view becomeFirstResponder,因爲您實際上是在self.view中添加了識別器。

3

我正在研究一個類似的應用程序,我可以輕鬆地在畫布上操作圖像。

1)檢查畫布的userInteractionEnabled屬性是否設置爲
2)您可以嘗試將手勢添加到ImageView的手勢而不是畫布。 (我猜你想縮放,旋轉n滑動圖像)。