2012-05-08 54 views
0

我有一個UIPopoverController,它是用自定義視圖控制器 - photoBoothController初始化的。UIPopovercontroller沒有檢測到觸摸

photoBoothController包含一個名爲canvas的UIView。這個畫布包含幾個gestureRecognizers,用於檢測手勢並執行適當的功能。

問題是,在顯示UIPopoverController時,gestureRecognizers未被識別。我已經通過添加一個UIButton來測試這個。如果我將它作爲photoBoothController的子視圖添加,我可以按下它。但是,如果我將它作爲畫布的子視圖添加,我不能再按下它。

所以我的問題是如何將手勢傳遞給畫布?我已經嘗試在photoBoothController的viewDidAppear和viewWillAppear方法中添加[canvas becomeFirstResponder],但它似乎沒有什麼區別。我知道代碼是完全正常的,因爲它在普通視圖下運行時非常完美,而不是在UIPopoverController中運行。

非常感謝。

守則photoBoothController:

#import "PhotoBoothController.h" 


@implementation PhotoBoothController 

@synthesize canvas; 
@synthesize photoImage; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - Private Methods 

-(void)showOverlayWithFrame:(CGRect)frame { 

    if (![_marque actionForKey:@"linePhase"]) { 
    CABasicAnimation *dashAnimation; 
    dashAnimation = [CABasicAnimation animationWithKeyPath:@"lineDashPhase"]; 
    [dashAnimation setFromValue:[NSNumber numberWithFloat:0.0f]]; 
    [dashAnimation setToValue:[NSNumber numberWithFloat:15.0f]]; 
    [dashAnimation setDuration:0.5f]; 
    [dashAnimation setRepeatCount:HUGE_VALF]; 
    [_marque addAnimation:dashAnimation forKey:@"linePhase"]; 
    } 

    _marque.bounds = CGRectMake(frame.origin.x, frame.origin.y, 0, 0); 
    _marque.position = CGPointMake(frame.origin.x + canvas.frame.origin.x, frame.origin.y + canvas.frame.origin.y); 

    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathAddRect(path, NULL, frame); 
    [_marque setPath:path]; 
    CGPathRelease(path); 

    _marque.hidden = NO; 

} 

-(void)scale:(id)sender { 

    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { 
     _lastScale = 1.0; 
    } 

    CGFloat scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]); 

    CGAffineTransform currentTransform = photoImage.transform; 
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale); 

    [photoImage setTransform:newTransform]; 

    _lastScale = [(UIPinchGestureRecognizer*)sender scale]; 
    [self showOverlayWithFrame:photoImage.frame]; 
} 

-(void)rotate:(id)sender { 

    if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) { 

     _lastRotation = 0.0; 
     return; 
    } 

    CGFloat rotation = 0.0 - (_lastRotation - [(UIRotationGestureRecognizer*)sender rotation]); 

    CGAffineTransform currentTransform = photoImage.transform; 
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation); 

    [photoImage setTransform:newTransform]; 

    _lastRotation = [(UIRotationGestureRecognizer*)sender rotation]; 
    [self showOverlayWithFrame:photoImage.frame]; 
} 


-(void)move:(id)sender { 
    NSLog(@"MOVING GESTURE"); 

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:canvas]; 

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { 
    _firstX = [photoImage center].x; 
    _firstY = [photoImage center].y; 
    } 

    translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y); 

    [photoImage setCenter:translatedPoint]; 
    [self showOverlayWithFrame:photoImage.frame]; 
} 

-(void)tapped:(id)sender { 
    _marque.hidden = YES; 
} 


#pragma mark - View lifecycle 

- (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]; 

} 

- (void)viewWillAppear:(BOOL)animated { 
    [self.canvas becomeFirstResponder]; 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidUnload 
{ 
    [self setPhotoImage:nil]; 
    [self setCanvas:nil]; 
    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return NO; 
} 

#pragma mark UIGestureRegognizerDelegate 

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

@end 

代碼初始化:

- (void)presentPhotoBoothForPhoto:(UIImage *)photo button:(UIButton *)button { 

    //Create frame outline image 
    UIImage *outline = [[UIImage imageNamed:@"HumptyLine1Frame.png"] adjustForResolution]; 
    UIImageView *outlineView = [[UIImageView alloc] initWithImage:outline]; 
    outlineView.frame = CGRectMake(0, 0, outline.size.width, outline.size.height); 

    //Create photo image. Already ajusted to resolution? 
    UIImageView *photoView = [[UIImageView alloc] initWithImage:photo]; 
    photoView.frame = CGRectMake(0, 0, photo.size.width, photo.size.height); 

    //Add done button 
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    doneButton.frame = CGRectMake(0, 0, 100, 40); 
    [doneButton addTarget:self action:@selector(renderPhoto:) forControlEvents:UIControlEventTouchUpInside]; 
    [doneButton setTitle:@"Done" forState:UIControlStateNormal]; 

    //Create canvas with outline as the frame into which the image is rendered 
    //Here is where I'll need to define a custom method to create a frame shaped by the outline 
    UIView *canvas = [[UIView alloc] initWithFrame:outlineView.frame]; 
    [canvas setBackgroundColor:[UIColor blackColor]]; 
    [canvas addSubview:photoView]; 
    [canvas addSubview:doneButton]; 

    //Create a photoBooth and set its contents 
    PhotoBoothController *photoBoothController = [[PhotoBoothController alloc] init]; 
    //resize the view 
    photoBoothController.contentSizeForViewInPopover = CGSizeMake(canvas.frame.size.width+100, canvas.frame.size.height+50); 
    photoBoothController.view.backgroundColor = [UIColor whiteColor]; 
    photoBoothController.canvas = canvas; 
    photoBoothController.photoImage = photoView; 
    [photoBoothController.view addSubview:canvas]; 
    //photoBoothController.view.gestureRecognizers = canvas.gestureRecognizers; 

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

回答

0

我覺得你的問題是在這裏

[photoBoothController.view addSubview:canvas]; 
[photoBoothController.view addSubview:outlineView]; 

你在canvasView加入outlineView無一不是有相同的框架..試試這個

[photoBoothController.view bringSubviewToFront:canvas]; 

只是一個例子:

TestVC *test = [[TestVC alloc] initWithNibName:@"TestVC" bundle:nil]; 

UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; 
[testView setBackgroundColor:[UIColor yellowColor]]; 

test.contentSizeForViewInPopover = CGSizeMake(testView.frame.size.width+100, testView.frame.size.height+50); 

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setFrame:CGRectMake(10, 10, 40, 40)]; 
[button setBackgroundColor:[UIColor redColor]]; 
[button addTarget:self action:@selector(sss) forControlEvents:UIControlEventTouchUpInside]; 

[testView addSubview:button]; 

[test.view addSubview:testView]; 

UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:test]; 
[popover presentPopoverFromRect:sender.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
+0

似乎並沒有作出了區別,不同之處在於框架現在是圖片後面。仍然不反對任何接觸和UIButton只適用於,如果它是一個photoBoothController的子視圖,而不是畫布......不知何故,我需要將photoBoothController觸摸傳遞到畫布,因爲它似乎阻止他們? – Smikey

+0

好的嘗試做分離的水平..添加按鈕到畫布和畫布到photoBoothController.view,不要添加任何東西,並檢查它是否工作。 –

+0

好的嘗試了,但按鈕仍然無法正常工作。只有當它直接作爲photoBoothController的子視圖添加時......我是否應該以某種方式傳遞手勢?只有我的photoBoothController符合UIGestureRecognizerDelegate ... – Smikey