如何在iphone中获取捏手势的坐标

时间:2011-08-08 11:56:24

标签: iphone ios objective-c uigesturerecognizer uipinchgesturerecognizer

我正在研究UIPinchGesture它的工作正常。我想在收缩时找到图像的坐标(x,y,宽度,高度),每次都要大。我的代码是:

- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender {

CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];
CGPoint point = [sender locationInView:self.moveImageView];
NSLog(@"The Coordinate is:--->  x = %f y = %f", point.x, point.y);
if (factor > 1) {

moveImageView.transform = CGAffineTransformMakeScale(lastScaleFactor +(factor-1),                                                          lastScaleFactor +(factor-1));                     } else {                         moveImageView.transform = CGAffineTransformMakeScale(lastScaleFactor * factor,                                                          lastScaleFactor * factor);

                    }

if (sender.state == UIGestureRecognizerStateEnded){
    if (factor > 1) {
        lastScaleFactor += (factor-1);
    } else {
        lastScaleFactor *= factor;

        }       
    }   

}

moveImageView是UIIMageView的对象。打印x和y的坐标,但我们还需要高度和宽度。

这项工作很好,我想在缩放(缩小和大)图像后找到图像的当前坐标(x,y,宽度,高度)。任何人都尽快帮助我。

谢谢你提前:)

2 个答案:

答案 0 :(得分:1)

- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender {

NSLog(@"image.frame.sizeWidth111 %f",moveImageView.frame.size.width);
NSLog(@"image.frame.sizeHeight111 %f",moveImageView.frame.size.height);

CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];
CGPoint point = [sender locationInView:self.moveImageView];

NSLog(@"The Coordinate is:--->  x = %f y = %f", point.x, point.y);

//如果当前因子大于1 - >放大

if (factor > 1) {
                moveImageView.transform = CGAffineTransformMakeScale(lastScaleFactor + (factor-1),
                                                     lastScaleFactor + (factor-1));
                } else {
                    moveImageView.transform = CGAffineTransformMakeScale(lastScaleFactor * factor, 
                                                     lastScaleFactor * factor);

                    }

if (sender.state == UIGestureRecognizerStateEnded){
    if (factor > 1) {
        lastScaleFactor += (factor-1);
    } else {
        lastScaleFactor *= factor;

        }       
    }   
NSLog(@"image.frame.sizeWidth222 %f",moveImageView.frame.size.width);
NSLog(@"image.frame.sizeHeight222 %f",moveImageView.frame.size.height);

}

答案 1 :(得分:0)

如果您在动画中需要任何精确状态的UIImageView的确切状态,则需要访问表示层。由于模型图层在动画期间未更新。这可以这样做 -

UIImageView *i    = (UIImageView *)sender;
CAlayer *imgLayer = [i.layer presentationLayer];

imgLayer具有您需要的精确坐标。包括尺寸(宽度,高度)。