使UIPinchGestureRecognizer和UIRotationGestureRecognizer协同工作

时间:2012-12-15 16:15:54

标签: 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.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");
}

然后我将.h文件和方法的委托添加到app delegate.m,但当时仍然只有一个手势。

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>

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

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

app delegate.m的方法你必须在你添加手势的类中添加方法,在你的情况下,viewcontroller.m

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

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