2015-08-13 96 views
0

我正在创建一个从底部向上滑动的菜单,并通过按下来激活动画的UIButton。但动画出现不正确。它应该像这样的工作 - https://youtu.be/79ZQDzzOHLk?t=2m52s(但在肖像模式)

但在这里它是如何工作后,我的编码是:
enter image description here动画未能正确显示

而这里的代码在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

它改成这样:

#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; 
    }]; 
}