开发者

Rotate a UIView on its X-axis(Horizontal axis)

开发者 https://www.devze.com 2023-04-11 07:12 出处:网络
I wanted to rotate a UIView on its horizontal axis for 360 degrees and then refresh the content in the view. I was looking out for solutions on this. Found a couple here n there. This is what I came o

I wanted to rotate a UIView on its horizontal axis for 360 degrees and then refresh the content in the view. I was looking out for solutions on this. Found a couple here n there. This is what I came out with.

    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
        self.tableView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0);
    } completion:^(BOOL finished){
        NSLog(@"Finished first pi");
        [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
            self.tableView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0);
        }  completion:^(BOOL finished) {
            NSLog(@"Finished second pi");

        }];           

    }];
开发者_开发百科

This flips the view but only by 180 degrees. I want it to flip one more time so that I can see the view normally..

Both my NSLogs are displayed one after the other. I am sure I am missing something here. ANy help would be helpful..

Thanks


In your completion block try concatenating the new transform with the current transform.

self.tableView.layer.transform = CATransform3DConcat(self.tableView.layer.transform, CATransform3DMakeRotation(M_PI,1.0,0.0,0.0));


You should check this answer How to make a CATransform3dMakeRotation rotate the other way? And chain together

I think that your problem is related with: "When you are working with a transform directly, Core Animation will interpolate the transform from the current value to the specified transform. It will find the shortest path to get to that transform, which will restrict the animation direction. If you try to animate the same transform property twice, the second value will simply override the first, not combine the two transforms together."


use this:-

  rotatingView.layer.anchorPoint = CGPointMake(0.0f, 0.0f);
    rotatingView.center = CGPointMake(0,
                                       (rotatingView.center.y -
                                        (rotatingView.bounds.size.height/2.0f)));
    rotatingView.center = CGPointMake(0,
                                      (rotatingView.center.y-
                                       (rotatingView.bounds.size.height/2)));

// start the Page Open
[UIView beginAnimations:@"Animation" context:nil];
[UIView setAnimationDuration:13.0];
// set angle as per requirement
[rotatingView.layer setValue:[NSNumber numberWithInt:280]
                   forKeyPath:@"transform.rotation.x"];

[UIView commitAnimations];


You just have to change your code from 1.0 to 0.0 inside completion block and woohoo all done.

 [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
            self.tableView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0);
        } completion:^(BOOL finished){
            NSLog(@"Finished first pi");
            [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
                self.tableView.layer.transform = CATransform3DMakeRotation(M_PI,0.0,0.0,0.0);
            }  completion:^(BOOL finished) {
                NSLog(@"Finished second pi");

            }];           

        }];


You can also approach this using the .Repeat and .Autoreverse animation options + setting animation's repeat count. Here's an example in Swift:

    UIView.animateWithDuration(time, delay: 0, options: [.Repeat, .Autoreverse], animations: {
        UIView.setAnimationRepeatCount(3)
        self.view.layer.transform = CATransform3DMakeRotation(CGFloat(M_PI), 1, 0, 0)
    }) { (completed) in
        // completion
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消