How do I animate/rotate a UIView 90 degress from its upper right corner?

前端 未结 3 1366
清歌不尽
清歌不尽 2021-02-02 04:01

I\'ve been searching for hours trying to find a way to animate/rotate a UIView 90 degrees from the upper right corner.

The effect should almost work like a swinging door

3条回答
  •  爱一瞬间的悲伤
    2021-02-02 04:20

    So right after I pressed enter I suddenly put two and two together and figured the Metronome sample worked kind of like a swinging door and that led me to a few other possibilities.

    Here's my solution:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // Set the anchor point and center so the view swings from the upper right
        swingView.layer.anchorPoint = CGPointMake(1.0, 0.0);
        swingView.center = CGPointMake(CGRectGetWidth(self.view.bounds), 0.0);
    
        // Rotate 90 degrees to hide it off screen
        CGAffineTransform rotationTransform = CGAffineTransformIdentity;
        rotationTransform = CGAffineTransformRotate(rotationTransform, DegreesToRadians(90));
        swingView.transform = rotationTransform;
    }
    
    ...
    
    - (void)animateSwing {
    
        CGAffineTransform swingTransform = CGAffineTransformIdentity;
        swingTransform = CGAffineTransformRotate(swingTransform, DegreesToRadians(0));
    
        [UIView beginAnimations:@"swing" context:swingView];
        [UIView setAnimationDuration:0.25];
    
        swingView.transform = swingTransform;
    
        [UIView commitAnimations];
    }
    

    Hope this helps someone else too!

提交回复
热议问题