0

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

photoBoothController 包含一个名为 canvas 的 UIView。此画布包含几个手势识别器,它们检测手势并执行适当的功能。

问题是当 UIPopoverController 显示时,gestureRecognizers 没有被识别。我已经通过添加 UIButton 对此进行了测试。如果我将它添加为 photoBoothController 的子视图,我可以按下它。但是,如果我将它添加为画布的子视图,我将无法再按下它。

所以我的问题是如何将手势传递给画布?我尝试添加[canvas becomeFirstResponder]photoBoothController 的 viewDidAppear 和 viewWillAppear 方法,但似乎没有什么不同。我知道代码在其他方面是合理的,因为它在作为常规视图而不是在 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];
}
4

1 回答 1

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];
于 2012-05-08T13:25:22.123 回答