2015-08-13 219 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; 
    }]; 
}