【问题标题】:making UIPinchGestureRecognizer and UIRotationGestureRecognizer working together使 UIPinchGestureRecognizer 和 UIRotationGestureRecognizer 一起工作
【发布时间】:2012-12-03 08:27:12
【问题描述】:

我尝试使用手势缩放和旋转 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 文件并将方法添加到应用程序委托.m,但当时仍然只有一个手势有效。

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>

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

有人可以帮我吗?

【问题讨论】:

  • 识别器总是一样有效吗?您是否检查过它们是否分别工作?即只添加它们。
  • 如果我开始捏合,它会缩放图像,但我不能同时捏合和旋转。如果我将开始旋转,然后尝试同时捏,那么再次只有旋转有效。

标签: objective-c xcode uiview uigesturerecognizer


【解决方案1】:

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

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

viewcontroller.m 中的这个方法不在 appdelegate.m 中

【讨论】:

  • 这就是解决方案!我不知道,该方法看起来应该在 appDelegate 中,但现在我知道我错了。谢谢你的回答。
猜你喜欢
  • 2015-08-21
  • 1970-01-01
  • 2021-12-23
  • 2012-04-02
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多