我正在创建一个菜单,该菜单通过按下以激活动画的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教程一样上下移动?
答案 0 :(得分:0)
将其更改为:
#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;
}];
}