5

这是我当前的手势识别器实现:

- (IBAction)handleRotate:(UIRotationGestureRecognizer *)recognizer {

if([recognizer state] == UIGestureRecognizerStateEnded) {

    _lastRotation = 0.0;
    return;
}

CGFloat rotation = 0.0 - (_lastRotation - [recognizer rotation]);

CGAffineTransform currentTransform = self.container.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

[self.container setTransform:newTransform];

_lastRotation = [recognizer rotation];

}

工作正常。问题是 self.container 总是围绕它的中心旋转。我希望它围绕两次触摸的中点旋转,这样如果您放大,您可以围绕您正在触摸的区域旋转。我该怎么做呢?

4

2 回答 2

0

我不确定您是否可以将一个点设置在被旋转对象的边界之外,但是您的容器层属性将允许您从变换中指定一个锚点。

它接受从左上角到右下角的 0.0-1.0 值。

#import <QuartzCore/QuartzCore.h>

[[container layer] setAnchorPoint:myCGPoint];
于 2012-08-20T07:22:32.463 回答
0

您可以使用代码将中心点放置到任何CGPoint类型变量

recognizer.view.center = CGPoint(x,y);

有关更多说明,您可以参考示例

(在 pangesturerecognizer 处理方法中,它显示了如何设置视图中心)

快乐编码:)

编辑 1

好的,我得到了如何在视图中获取触摸位置的代码(任何),因为首先需要的是为视图启用多点触摸属性。(见下图)

在 XIB 中启用多点触控

然后实现委托方法如下

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Began %@",touches);
    NSUInteger touchCount = 0;
    for(UITouch *tu in touches)
    {
        CGPoint pt = [tu locationInView:self.view];
        NSLog(@"pt %f, %f",pt.x,pt.y);
        touchCount ++;
    }
    NSLog(@"touchCount %d",touchCount);
}

此方法显示NSLog. 我self.view仅用于演示,您可以使用任何视图。

根据您的要求修改此方法,并使用以下计算使用两点坐标获取中心点

p3.x = (p1.x + p2.x)/2;p3.y = (p1.y + p2.y)/2;

这里 p1,p2 是您在上述委托方法中获得的两个接触点,而 p3 是您需要的中心点。

快乐编码:)

于 2012-08-20T13:31:36.173 回答