修复弹出滚动菜单的动画

Fix animation of pop-up scroll menu

我正在尝试通过激活 UI 按钮制作一个从按钮向上滑动的菜单。当你按下按钮时,一个 UIscrollView 从底部向上滑动。当 UI 按钮激活和未激活时,该按钮也会停留在幻灯片菜单上方。

它应该是这样的:

我在实际项目中将屏幕锁定为纵向模式,问题是当您按下按钮时。 UIscrollView 滑动到屏幕中间,再次按下按钮时,它只会向下移动几下。而且按钮不会 WITH UIscrollView 移动。我想让 UI scrollView 和 Unbutton 像上图一样出现,然后在屏幕视口 下下降。

这就是我的实际项目的样子 - http://s4.postimg.org/dlvxnffbx/unnamed.png


这是来自 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

你试过替换:

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, 200, 60, 30);

    [self.view bringSubviewToFront:scrollView];

    [UIView commitAnimations];
}

与 :

    if (draw1 ==0) {
        draw1 = 1;
        [UIView animateWithDuration:0.5
                              delay:0.0
                            options: UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             scrollView.frame = CGRectMake(0, 1000, 568, 55);
                             openMenu.frame = CGRectMake(220, 200, 60, 30);
                         } 
                         completion:^(BOOL finished){
                             NSLog(@"Done!");
                         }];
    } else {
        draw1 = 0;
        [UIView animateWithDuration:0.5
                              delay:0.0
                            options: UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             scrollView.frame = CGRectMake(0, 300, 568, 55);
                             openMenu.frame = CGRectMake(220, 270, 60, 30);
                         } 
                         completion:^(BOOL finished){
                             NSLog(@"Done!");
                         }];
    }

如果您正在寻找它应该从按钮滑动,意味着开始时它的帧大小应该为零并开始从按钮中心出现并且应该达到全尺寸那么您应该像这样在动画之前设置帧

下面的代码会在您想要打开时从屏幕底部调出 scrollView,而在关闭时它将隐藏屏幕底部的 scrollView...

在 viewDidLoad 方法中设置你想要的初始框架...如果你想让它隐藏然后设置 scrollView 的 y 位置大于 superView 的高度,在这种情况下它将隐藏视图在底部。

(draw) {
    // to open
    draw = 0;
    [UIView animateWithDuration:1 animations:^{
        scrollView.frame = CGRectMake(0, self.view.bounds.size.height - 55, self.view.bounds.size.width, 55);
        openMenu.frame = CGRectMake(220, scrollView.frame.origin.y - 40, 60, 30);

    }];
}
else
{
    //close
    draw = 1;
    [UIView animateWithDuration:1 animations:^{
        CGRectMake(0, self.view.bounds.size.height + 55, self.view.bounds.size.width, 55);
        openMenu.frame = CGRectMake(220, scrollView.frame.origin.y - 40, 60, 30);

    }];
}