Objective c 使UIPinchGestureRecognitor和UIRotationGestureRecognitor协同工作

Objective c 使UIPinchGestureRecognitor和UIRotationGestureRecognitor协同工作,objective-c,xcode,uiview,uigesturerecognizer,Objective C,Xcode,Uiview,Uigesturerecognizer,我尝试使用手势缩放和旋转UIImageView。我在互联网上看到了几个例子,我已经实现了它,但它不能一起工作 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.

我尝试使用手势缩放和旋转UIImageView。我在互联网上看到了几个例子,我已经实现了它,但它不能一起工作

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
   // imgView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    imgView.image = [UIImage imageNamed:@"spotItInLondonIcon.png"];
    [imgView setContentMode:UIViewContentModeScaleAspectFit];
    [imgView setMultipleTouchEnabled:YES];
    [imgView setUserInteractionEnabled:YES];
    [self.view addSubview:imgView];

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    [imgView addGestureRecognizer:pinch];
    pinch.delegate = self;

    UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
    [imgView addGestureRecognizer:rotate];
    rotate.delegate = self;
}
-(void)pinch: (UIPinchGestureRecognizer*)sender
{
    CGFloat scale = sender.scale;
    imgView.transform = CGAffineTransformScale(imgView.transform, scale, scale);
    sender.scale = 1.0;
    NSLog(@"pinch executed");
}
-(void)rotation: (UIRotationGestureRecognizer*)rotationDetected
{
    CGFloat angle = rotationDetected.rotation;
    imgView.transform = CGAffineTransformRotate(imgView.transform, angle);
    rotationDetected.rotation = 0.0;
    NSLog(@"rotation executed");
}
然后我将delegate添加到.h文件中,并将方法添加到app delegate.m中,但此时仍然只有一个手势有效

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
@界面ViewController:UIViewController
-(BOOL)手势识别器:(UIGestureRecognizer*)手势识别器应与gestureRecognizer:(UIGestureRecognizer*)其他手势识别器同时识别
{
返回YES;
}

有人能帮我吗?

将方法添加到app delegate.m??您必须将该方法添加到要添加手势的类中,在您的例子中是viewcontroller.m

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

viewcontroller.m中的此方法不在appdelegate.m中

是否始终使用相同的识别器?你有没有检查过它们是分开工作的?i、 e.只添加它们。如果我开始挤压,它将缩放图像,但我不能同时挤压和旋转。如果我将开始旋转,然后同时尝试挤压,那么再次只有旋转起作用。这就是解决方案!我不知道,这个方法看起来应该在appDelegate中,但现在我知道我是我的错。谢谢你的回答。