如何更改Pan Gesture上的3D旋转?

时间:2013-09-19 09:13:16

标签: iphone rotation swipe iso uipangesturerecognizer

我已经完成动画打开页面,但现在我必须为Pan Gesture更改它。我该怎么开始?

 [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
    CALayer *layer = ges.view.layer;
    CATransform3D initialTransform = ges.view.layer.transform;
    initialTransform.m34 = 1.0 / -1100;
    layer.transform = initialTransform;
    layer.anchorPoint = CGPointMake(-0.0, 0.5);
    [UIView beginAnimations:@"Scale" context:nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    CATransform3D rotationAndPerspectiveTransform = ges.view.layer.transform;
    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, -M_PI , 0 , -ges.view.bounds.size.height/2, 0);
    ges.view.transform = CGAffineTransformRotate(ges.view.transform, 0);
    layer.transform = rotationAndPerspectiveTransform;
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];

2 个答案:

答案 0 :(得分:1)

UIPanGestureRecognizer myPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(imagePanned:)];
[_panGesture setMaximumNumberOfTouches:1];
[yourView addGestureRecognizer:myPanGesture];

1)如果您希望在用户移动视图时页面与平移操作一起转动,那么您必须这样做。

- (void)imagePanned:(UIPanGestureRecognizer *)iRecognizer {
    CGPoint translation = [recognizer translationInView:self.view];

    //Calculate transformation based on the translation value of pan gesture, this will be a tricky part

    [UIView animateWithDuration:0.5 delay: 0 options: 0 animations:
     ^(void) {
        //Apply transformation
     }
    completion: nil];
}

希望这会对您有所帮助。

编辑:扩展答案 * 这与第一个想法 *

有关

您只想将视图旋转到右边? 然后使用CATransform3D,在这里您需要计算我们申请查看的iAngle

iAngle = //Calculate based on translation 

翻译在哪里

CGPoint translation = [recognizer translationInView:self.view];

然后应用转换来查看

CATransform3D myTransform = CATransform3DIdentity;
myTransform.m34 = 1.0 / -500;
myTransform = CATransform3DRotate(myTransform, DEGREES_TO_RADIANS(-iAngle), 0.0f, 1.0f, 0.0f);   //#define DEGREES_TO_RADIANS(d) (d * M_PI / 180)
yourView.layer.transform = myTransform;

答案 1 :(得分:0)

我会给你数学背后的数学,因为我现在没有代码显示。将手指移动转换为旋转值只需设置比率。

对于角度值(用于转换):

                track_spread     rotation_angle
                ____________  =  ______________

                 movement_x         angle = ?

angle = (rotation_angle * movement_x)/track_spread;

在哪里:

track_spread   = width_of_your_view (or less e.g. 70% x width_of_your_view)
rotation_angle = the final rotation you want for your view (a constant e.g. 45 degrees)
CGPoint point = [recognizer translationInView:self];
movement_x = abs(point.x); 
angle = the value (in degrees) you are interested in and will input into transform after converting to radians

如果有人无法弄清楚并且仍然需要代码段,请发表评论。