动画未正确显示
Posted
技术标签:
【中文标题】动画未正确显示【英文标题】:Animation is not coming out correctly 【发布时间】:2015-08-13 20:37:59 【问题描述】:我正在创建一个菜单,该菜单通过按下以激活动画的 UIButton 从底部向上滑动。但是动画出来不正确。它应该像这样工作 -https://youtu.be/79ZQDzzOHLk?t=2m52s(但在纵向模式下)但这是我编码后的工作方式:
这是 viewcontroller.m 中的动画代码
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize scrollView;
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
draw1 = 0;
scrollView.frame = CGRectMake(0, 300, 480, 55);
[scrollView setContentSize:CGSizeMake(480, 55)];
openMenu.frame = CGRectMake(220, 270, 60, 30);
- (IBAction)OpenMenu:(id)sender
if (draw1 ==0)
draw1 = 1;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationDuration:0.5];
[UIButton setAnimationDelay:0.0];
[UIButton setAnimationCurve:UIViewAnimationCurveEaseOut];
scrollView.frame = CGRectMake(0, 245, 568, 55);
openMenu.frame = CGRectMake(220, 200, 60, 30);
[self.view bringSubviewToFront:scrollView];
[UIView commitAnimations];
else
draw1 = 0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIButton beginAnimations:nil context:nil];
[UIButton setAnimationDuration:0.5];
[UIButton setAnimationDelay:0.0];
[UIButton setAnimationCurve:UIViewAnimationCurveEaseOut];
scrollView.frame = CGRectMake(0, 300, 568, 55);
openMenu.frame = CGRectMake(220, 270, 60, 30);
[self.view bringSubviewToFront:scrollView];
[UIView commitAnimations];
@end
如何解决这个问题,以便 UIButton 以纵向模式出现在屏幕底部,并且像 Youtube 教程中一样上下移动?
【问题讨论】:
【参考方案1】:改成这样:
#import "ViewController.h"
#define SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
#define SCREEN_HEIGHT ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)
@interface ViewController ()
@end
@implementation ViewController
@synthesize scrollView;
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
draw1 = 0;
scrollView.frame = CGRectMake(0, SCREEN_HEIGHT-55, SCREEN_WIDTH, 55);
[scrollView setContentSize:CGSizeMake(SCREEN_WIDTH, 55)];
[scrollView setContentSize:CGSizeMake(480, 55)];
- (IBAction)OpenMenu:(id)sender
if (CGAffineTransformIsIdentity(scrollView.transform))
[UIView animateWithDuration:0.5 animations:^
scrollView.transform = CGAffineTransformMakeTranslation(0, 55);
];
else
[UIView animateWithDuration:0.5 animations:^
scrollView.transform = CGAffineTransformIdentity;
];
@end
或者,把上面的这个方法改成下面的(都是一样的,只是更优雅更简单):
- (IBAction)OpenMenu:(id)sender
到这里:
- (IBAction)OpenMenu:(id)sender
[UIView animateWithDuration:0.5 animations:^
scrollView.transform = CGAffineTransformIsIdentity(scrollView.transform) ?
CGAffineTransformMakeTranslation(0, 100):CGAffineTransformIdentity;
];
【讨论】:
以上是关于动画未正确显示的主要内容,如果未能解决你的问题,请参考以下文章