0

我已经完成了打开页面的动画,但现在我必须将其更改为 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];
4

2 回答 2

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;
于 2013-09-19T09:36:08.507 回答
0

I'll give you the math behind doing it as I don't have code right now to show. Converting finger movement to rotation value is simply a matter of setting up ratios.

For angle value (used in transform):

                track_spread     rotation_angle
                ____________  =  ______________

                 movement_x         angle = ?

angle = (rotation_angle * movement_x)/track_spread;

WHERE:

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

If someone cant figure out and still need code snippet please leave a comment.

于 2014-11-08T19:40:59.507 回答