Pinch手势不能与imageVIew一起使用

时间:2013-03-25 13:31:26

标签: iphone objective-c xcode cocoa-touch

我正在尝试放大和缩小imageView

这是代码

- (void)pinch:(UIPinchGestureRecognizer *)gesture
{
   if (handSelected == YES)
   {
    if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateChanged)
    {
        NSLog(@"gesture.scale = %f", gesture.scale);

        CGFloat currentScale = self.imgHand.frame.size.width / self.imgHand.bounds.size.width;
        CGFloat newScale = currentScale * gesture.scale;

        if (newScale < 1.0) {
            newScale = 1.0;
        }
        if (newScale > 4.0) {
            newScale = 4.0;
        }

        CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
        self.imgHand.transform = transform;
        gesture.scale = 1;
    }
   }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self adjustRingPressed:self];
    self.view.multipleTouchEnabled = YES;
    self.imgHand.multipleTouchEnabled = YES;
    UIPinchGestureRecognizer *gst = [[UIPinchGestureRecognizer alloc] initWithTarget:self.imgHand action:@selector(pinch:)];
    [gst setDelegate:self];
    [self.imgHand addGestureRecognizer:gst];

}

似乎我的捏代码永远不会运行

2 个答案:

答案 0 :(得分:2)

尝试添加:

self.imgHand.userInteractionEnabled = TRUE;

默认情况下,UIImageView具有userInteractionEnabled = FALSE

答案 1 :(得分:0)

使其成为gesture的函数,如下面的模板代码:

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1;
}