Kei*_*ald 142 iphone cocoa-touch core-animation uiview ios
在iPhone上设置动画视图过渡的最佳做法是什么?
例如,ViewTransitions来自apple 的示例项目使用如下代码:
CATransition *applicationLoadViewIn = [CATransition animation];
[applicationLoadViewIn setDuration:1];
[applicationLoadViewIn setType:kCATransitionReveal];
[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[[myview layer] addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];
Run Code Online (Sandbox Code Playgroud)
但是网络上还有漂浮的代码片段,如下所示:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];
[myview removeFromSuperview];
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)
什么是最好的方法?如果你也可以提供一个片段,那就非常感激了.
注意:我无法让第二种方法正常工作.
Raf*_*ega 118
从UIView参考的有关beginAnimations:context:方法的部分:
在iPhone OS 4.0及更高版本中不鼓励使用此方法.您应该使用基于块的动画方法.
例如基于Tom的评论的基于块的动画
[UIView transitionWithView:mysuperview
duration:0.75
options:UIViewAnimationTransitionFlipFromRight
animations:^{
[myview removeFromSuperview];
}
completion:nil];
Run Code Online (Sandbox Code Playgroud)
mah*_*udz 69
我一直在使用后者来制作很多不错的轻量级动画.您可以使用它交叉淡化两个视图,或者将一个视图淡入另一个视图,或淡出它.你可以像横幅一样拍摄另一个视图,你可以使视图拉伸或缩小......我从beginAnimation/ 获得了很多里程commitAnimations.
不要以为你所能做的就是:
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];
Run Code Online (Sandbox Code Playgroud)
这是一个示例:
[UIView beginAnimations:nil context:NULL]; {
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
if (movingViewIn) {
// after the animation is over, call afterAnimationProceedWithGame
// to start the game
[UIView setAnimationDidStopSelector:@selector(afterAnimationProceedWithGame)];
// [UIView setAnimationRepeatCount:5.0]; // don't forget you can repeat an animation
// [UIView setAnimationDelay:0.50];
// [UIView setAnimationRepeatAutoreverses:YES];
gameView.alpha = 1.0;
topGameView.alpha = 1.0;
viewrect1.origin.y = selfrect.size.height - (viewrect1.size.height);
viewrect2.origin.y = -20;
topGameView.alpha = 1.0;
}
else {
// call putBackStatusBar after animation to restore the state after this animation
[UIView setAnimationDidStopSelector:@selector(putBackStatusBar)];
gameView.alpha = 0.0;
topGameView.alpha = 0.0;
}
[gameView setFrame:viewrect1];
[topGameView setFrame:viewrect2];
} [UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)
如您所见,您可以使用Alpha,框架甚至是视图的大小.玩转.您可能对其功能感到惊讶.
Rya*_*aig 52
差异似乎是动画需要的控制量.
该CATransition方法为您提供更多控制,因此可以设置更多内容,例如.计时功能.作为一个对象,您可以将其存储以供日后使用,重构以将所有动画指向它以减少重复的代码等.
在UIView类方法是常见的动画方便的方法,但比比较有限CATransition.例如,只有四种可能的过渡类型(向左翻转,右翻,向上卷曲,向下卷曲).如果你想淡入淡出,你必须深入研究CATransition's淡入过渡,或者设置你UIView的alpha 的明确动画.
请注意,CATransition在Mac OS X上,您可以指定一个任意CoreImage过滤器用作过渡,但就像现在一样,您无法在缺少iPhone的iPhone上执行此操作CoreImage.
Gur*_*uru 26
我们可以使用这个简单的代码在ios 5中为图像制作动画.
CGRect imageFrame = imageView.frame;
imageFrame.origin.y = self.view.bounds.size.height;
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationCurveEaseOut
animations:^{
imageView.frame = imageFrame;
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
Run Code Online (Sandbox Code Playgroud)
在UIView文档中,请阅读有关ios4 +的此功能
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
Run Code Online (Sandbox Code Playgroud)
无论如何,"阻止"方法现在是优先考虑的.我将在下面解释简单的块.
考虑下面的剪辑.bug2和bug 3是imageViews.下面的动画描述了延迟1秒后持续1秒的动画.bug3从它的中心移动到bug2的中心.动画完成后,将记录"Center Animation Done!".
-(void)centerAnimation:(id)sender
{
NSLog(@"Center animation triggered!");
CGPoint bug2Center = bug2.center;
[UIView animateWithDuration:1
delay:1.0
options: UIViewAnimationCurveEaseOut
animations:^{
bug3.center = bug2Center;
}
completion:^(BOOL finished){
NSLog(@"Center Animation Done!");
}];
}
Run Code Online (Sandbox Code Playgroud)
希望干净!!!
| 归档时间: |
|
| 查看次数: |
213352 次 |
| 最近记录: |