ios 如何从右上角动画/旋转 UIView 90 度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4072460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:59:58  来源:igfitidea点击:

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

iosuiviewrotationcore-animationtransform

提问by dandax

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

我已经搜索了几个小时,试图找到一种从右上角动画/旋转 UIView 90 度的方法。

The effect should almost work like a swinging door from the top of the screen.

效果应该几乎像屏幕顶部的旋转门一样。

Hope someone can help!

希望有人能帮忙!

回答by dandax

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.

所以在我按下 Enter 键后,我突然将两个和两个放在一起,发现节拍器样本的工作方式有点像一扇旋转门,这让我想到了其他一些可能性。

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!

希望这对其他人也有帮助!

回答by tadejsv

You should try setting the anchor point of the layer to (0,1), and than animate the layer.

您应该尝试将图层的锚点设置为 (0,1),然后为图层设置动画。

回答by nghiamas

You should try this code:

你应该试试这个代码:

-(void)doRotationView{
[UIView beginAnimations:@"flipping view" context:nil];  
    [UIView setAnimationDuration:1];    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];   
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft    
                           forView:self cache:YES];
    if (flagFront == 1) {
        flagFront =0;
        [self.viewSecond setHidden:NO];
        [self.viewFirst setHidden:YES];
    }
    else{
        flagFront =1;
        [self.viewSecond setHidden:YES];
        [self.viewFirst setHidden:NO];        
    }
    [UIView commitAnimations];

}

}