【问题标题】: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 教程中一样上下移动?

【问题讨论】:

    标签: ios objective-c xcode animation xcode6


    【解决方案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;
         }];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 2022-10-17
      • 1970-01-01
      相关资源
      最近更新 更多