UIPopovercontroller没有检测到触摸

时间:2012-05-08 13:18:34

标签: objective-c ios xcode uipopovercontroller

我有一个UIPopoverController,它使用自定义视图控制器初始化 - photoBoothController。

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

1 个答案:

答案 0 :(得分: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];