How to change for 3D rotation on UIPanGesture?

柔情痞子 提交于 2019-12-08 13:34:32

问题


I have done animation opening a page on tapping on it but now I have to change it for Pan Gesture. How should I start?

 [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];

回答1:


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

1) if you want page to turn along with the pan action as user moves the view then you have to do this way.

- (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];
}

Hope this will help you.

Edit : Extended Answer *This goes with the first idea*

You just want to rotate the view right ? then use CATransform3D, here you will need to calculate the iAngle that we are applying to view.

iAngle = //Calculate based on translation 

where translation is

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

Then apply transformation to 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;



回答2:


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.



来源:https://stackoverflow.com/questions/18890624/how-to-change-for-3d-rotation-on-uipangesture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!