xcode UIView 动画滑入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13891652/
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-09-15 02:20:16  来源:igfitidea点击:

UIView Animation slide in

iosxcodeanimationuiviewcgrect

提问by user1432813

I am trying to get my UIViewto slide in from the right in the viewDidLoadmethod here's what I've got.

我试图让我UIViewviewDidLoad方法从右侧滑入,这就是我所拥有的。

CGRect menuFrame = self.menu.frame;
menuFrame.origin.y = menuFrame.size.height+200;

[UIView animateWithDuration:1
                      delay:0.05
                    options: UIViewAnimationCurveEaseIn
                 animations:^{
                     self.menu.frame = menuFrame;
                 }
                 completion:^(BOOL finished){
                     NSLog(@"Animation Complete!");

                 }];

I am not sure what has gone wrong here I appreciate any help greatly.

我不确定这里出了什么问题,我非常感谢任何帮助。

回答by matt

This isn't what you asked, but it is worth saying anyway: viewDidLoadis a bad place for this code. viewDidLoadis called because the view controller has obtained its view, not because that view has appeared or will soon appear in the interface. It mightsoon appear in the interface, which is why your code has seemed to work up until now, but it might not.

这不是您问的问题,但无论如何值得说:viewDidLoad此代码的位置不好。viewDidLoad被调用是因为视图控制器已经获得了它的视图,而不是因为该视图已经出现或即将出现在界面中。它可能很快就会出现在界面中,这就是为什么您的代码直到现在似乎都能正常工作,但也可能不会。

The correct place for this code is probably viewWillAppearor viewDidAppear. You probably want to call this code only once, though, the first time the view appears. If you want to prevent the code from being called, because you've already animated menuinto view on an earlier viewDidAppearcall, simply include a conditional check to see whether menualready has a frame within the visible view.

此代码的正确位置可能是viewWillAppearviewDidAppear。但是,您可能只想在第一次出现视图时调用此代码一次。如果您想阻止代码被调用,因为您已经menu在之前的viewDidAppear调用中动画进入视图,只需包含一个条件检查以查看menu可见视图中是否已经有一个框架。

By the way, another reason for avoiding things having to do with frames and bounds in viewDidLoadis that if your app launches into landscape and this is your root view, xand yare reversed.

顺便说一句,避免与框架和边界有关的另一个原因viewDidLoad是,如果您的应用程序启动到横向并且这是您的根视图,x并且y被反转。

回答by Abizern

It looks like you are trying to slide your menu in from the bottom as the Y position is pushed down initially by 200.

看起来您正试图从底部滑入菜单,因为 Y 位置最初向下推 200。

Usually you start by adding the view as a subview in its offscreen position and then set the onscreen position in the animation block.

通常,您首先将视图作为子视图添加到其屏幕外位置,然后在动画块中设置屏幕位置。

And, make sure that you pass 1.0as the animation duration.

并且,确保你1.0作为动画持续时间传递。

回答by ganesh manoj

use those transition.subtype for different effects

将这些 transition.subtype 用于不同的效果

CATransition* transition = [CATransition animation];  
transition.duration = 0.5;  
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
transition.subtype = kCATransitionFromLeft; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
//transition.subtype = kCATransitionFromTop; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom

[appDelegate.navigation.view.layer addAnimation:transition forKey:nil];
viewController *DairyVC = [[viewController alloc] initWithNibName:@"viewController" bundle:nil];

[appDelegate.navigation pushViewController:DairyVC animated:YES];