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
UIView Animation slide in
提问by user1432813
I am trying to get my UIView
to slide in from the right in the viewDidLoad
method here's what I've got.
我试图让我UIView
的viewDidLoad
方法从右侧滑入,这就是我所拥有的。
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: viewDidLoad
is a bad place for this code. viewDidLoad
is 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 viewWillAppear
or 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 menu
into view on an earlier viewDidAppear
call, simply include a conditional check to see whether menu
already has a frame within the visible view.
此代码的正确位置可能是viewWillAppear
或viewDidAppear
。但是,您可能只想在第一次出现视图时调用此代码一次。如果您想阻止代码被调用,因为您已经menu
在之前的viewDidAppear
调用中动画进入视图,只需包含一个条件检查以查看menu
可见视图中是否已经有一个框架。
By the way, another reason for avoiding things having to do with frames and bounds in viewDidLoad
is that if your app launches into landscape and this is your root view, x
and y
are 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.0
as 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];