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

时间:2013-12-16 13:32:53

标签: ios objective-c uigesturerecognizer

我在ViewController中有一个ImageView,我想用Pinch Gesture放大或缩小。此外,我想在用户双击图像时获取初始视图。我遵循了本教程Make Gallery

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

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

这些是处理事件的方法:

@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 个答案:

答案 0 :(得分:1)

我不得不将变量 scale 重置为1.0到resetImage方法。所以现在它正常运作。