2

我尝试使用手势缩放和旋转 UIImageView。我在 Internet 上看到了几个示例并且我已经实现了它,但是它不能一起工作。

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

有人能帮我吗?

4

1 回答 1

3

应用程序 delegate.m 的方法??您必须在要添加手势的类中添加方法,在您的情况下,viewcontroller.m

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

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

于 2012-12-16T09:27:48.860 回答