捏合和点击手势无法正常工作

Posted

技术标签:

【中文标题】捏合和点击手势无法正常工作【英文标题】:Pinch and Tap Gestures don't work properly 【发布时间】:2013-12-16 13:32:52 【问题描述】:

我在 ViewController 中有一个 ImageView,我想用捏手势放大或缩小。另外,我想在用户双击图像时获得初始视图。我按照本教程进行操作Make Gallery

如果我只使用捏合手势都可以正常工作,我可以放大直到达到最大缩放,我可以缩小直到达到最小缩放。正确的结果如下图所示。

虽然,如果我双击 ImageView,ImageView 会获得其初始格式(图 1),但无法识别最小缩放的实现,如果我继续使用捏合手势缩小,ImageView 会继续缩小,我得到此结果(图 2):

这些是我处理事件的方法:

@interface ImageGalleryViewController ()


NSString *productName;
CGFloat previousScale;
CGFloat previousRotation;
CGFloat beginX;
CGFloat beginY;

CGFloat scale;
//CGFloat __previousScale;


- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.

   scale=1.0;

   UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]       initWithTarget:self action:@selector(scaleImage:)];
   [self.view addGestureRecognizer:pinchGesture];

   UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resetImage:)];
   tapGesture.numberOfTapsRequired=2;
   [self.view addGestureRecognizer:tapGesture];




 - (void)resetImage:(UITapGestureRecognizer *)recognizer
     [UIView animateWithDuration:0.3 animations:^() 

     self.view.transform = CGAffineTransformIdentity;

     ];
     self.bigImage.transform = CGAffineTransformIdentity;
     [self.bigImage setCenter: CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2+32)];
     [UIView commitAnimations];
 

- (void)scaleImage:(UIPinchGestureRecognizer *)recognizer

     NSLog(@"Scale: %f", [recognizer scale]);
     if ([recognizer state] == UIGestureRecognizerStateBegan) 
        previousScale = scale;
     

     CGFloat currentScale = MAX(MIN([recognizer scale] * scale, 10), 1);
     CGFloat scaleStep = currentScale / previousScale;
     [self.view setTransform: CGAffineTransformScale(self.view.transform, scaleStep, scaleStep)];

    previousScale = currentScale;

    if ([recognizer state] == UIGestureRecognizerStateEnded ||
        [recognizer state] == UIGestureRecognizerStateCancelled ||
        [recognizer state] == UIGestureRecognizerStateFailed) 
    // Gesture can fail (or cancelled?) when the notification and the object is dragged simultaneously
        scale = currentScale;
        NSLog(@"Final scale: %f", scale);
    

如果初始右视图(第一张图像)已经实现,我该如何停止缩小?

【问题讨论】:

【参考方案1】:

我不得不在 resetImage 方法中将变量 scale 重置为 1.0。所以现在它可以正常工作了。

【讨论】:

以上是关于捏合和点击手势无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

UIImageView 上的捏合、平移和双击手势

带有平移、旋转和捏合手势的 UIKit Dynamics

三指捏合手势

平移手势无法正常工作

点击手势不起作用

如果视图注册平移手势,则捏合手势不起作用