【问题标题】:Animation speed inherited from first View Controller从第一个 View Controller 继承的动画速度
【发布时间】:2013-12-13 21:08:50
【问题描述】:

我在学习 Xcode 时正在试验,我创建了一个带有菜单视图控制器的应用程序,然后链接到 3 个单独的视图控制器的地图。

所有 3 个都按照我的意愿完美运行,都在触摸时制作不同的动画,所以一切都很好。

我想添加一个弹出菜单,我按照本指南 http://www.samsurge.com/developer/apple/pop-up-menu.php 将其添加到我的 mapOne 视图控制器中进行试用。

公平地说,该指南有效,并且完全符合我的要求。由于使用此代码而出现的问题是,现在 mapTwo 视图控制器上的动画现在使用 0.3 的速度进行动画处理,该速度用于动画进出弹出菜单。

只有在我实际使用 mapOne 视图控制器上的弹出菜单时才会发生这种情况。

因此,如果我先使用 mapTwo 视图控制器,它会正常工作。

如果我先去 mapOne 视图控制器并且不使用弹出菜单并返回到 mapTwo 视图控制器,那么那里的动画会正常工作。

但是,如果我先转到 mapOne 视图控制器并单击弹出菜单,然后返回到 mapTwo 视图控制器,所有的动画都以 0.3 秒的速度进行动画处理,这是 mapOne 视图控制器上弹出菜单的动画速度。

有人知道这是什么情况吗?

这是我的 mapOne 视图控制器的代码,其中包括弹出菜单的代码:

#import "GWSMapOneViewController.h"

@interface GWSMapOneViewController ()

@end

@implementation GWSMapOneViewController

@synthesize pandaBigButton;
@synthesize pandaSmallButton;
@synthesize scrollView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Pop Up Menu

    draw1 = 0;
    scrollView.frame = CGRectMake(0, 568, 320, 0);
    [scrollView setContentSize:CGSizeMake(320, 0)];
    openMenu.frame = CGRectMake(124, 496, 72, 72);

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)pandaBigButton_clicked:(id)sender
{

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Super_Mario_Bros_Jump", CFSTR ("mp3"), NULL);

    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

    // Big Panda Animation

    pandaBigButton.imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"panda-png-happy"],[UIImage imageNamed:@"panda-png-chewing"],nil];

    pandaBigButton.imageView.animationDuration = 1.0;
    pandaBigButton.imageView.animationRepeatCount = 1;
    [pandaBigButton.imageView startAnimating];
}

- (IBAction)pandaSmallButton_clicked:(id)sender
{
    SystemSoundID soundID;
    NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"Super_Mario_Bros_Mushroom" ofType:@"mp3"];

    AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile], & soundID);
    AudioServicesPlaySystemSound(soundID);

    pandaSmallButton.imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"panda-png-chewing"],[UIImage imageNamed:@"panda-png-happy"],nil];

    pandaSmallButton.imageView.animationDuration = 0.2;
    pandaSmallButton.imageView.animationRepeatCount = 6;
    [pandaSmallButton.imageView startAnimating];
}

- (IBAction)OpenMenu:(id)sender {
    if (draw1 == 0) {
        draw1 = 1;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

        [UIButton beginAnimations:nil context:nil];
        [UIButton setAnimationDuration:0.3];
        [UIButton setAnimationDelay:0.0];
        [UIButton setAnimationCurve:UIViewAnimationCurveEaseOut];

        scrollView.frame = CGRectMake(0, 468, 320, 100);
        openMenu.frame = CGRectMake(124, 396, 72, 72);

        [self.view bringSubviewToFront:scrollView];

        [UIView commitAnimations];
    } else {
        draw1 = 0;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

        [UIButton beginAnimations:nil context:nil];
        [UIButton setAnimationDuration:0.3];
        [UIButton setAnimationDelay:0.0];
        [UIButton setAnimationCurve:UIViewAnimationCurveEaseOut];

        scrollView.frame = CGRectMake(0, 568, 320, 0);
        openMenu.frame = CGRectMake(124, 498, 72, 72);

        [self.view bringSubviewToFront:scrollView];

        [UIView commitAnimations];
    }
}



@end

被覆盖的 mapTwo 视图控制器在这里:

#import "GWSMapTwoViewController.h"

@interface GWSMapTwoViewController ()

@end

@implementation GWSMapTwoViewController
@synthesize cloudAnimate;
@synthesize cloudAnimate1;
@synthesize marioRunning;
@synthesize bowserJump;
@synthesize sun;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Move UIView

    cloudAnimate.center = CGPointMake(400.0, 90.0);
    cloudAnimate1.center = CGPointMake(400.0, 150.0);
    marioRunning.center = CGPointMake(160.0, 456.0);
    sun.center = CGPointMake(-75.0, 235.0);
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidLoad];


    [UIView animateWithDuration:10.0
                          delay:1.0
                        options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         cloudAnimate.center = CGPointMake(-100.0, 90.0);
                     }
                     completion:NULL];
    [UIView animateWithDuration:8.0
                      delay:0.0
                    options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     cloudAnimate1.center = CGPointMake(-100.0, 150.0);
                 }
                 completion:NULL];

    [UIView animateWithDuration:30.5
                          delay:0.0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         sun.center = CGPointMake(395.0, 100.0);
                     }
                     completion:NULL];

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


- (IBAction)marioRunning_clicked:(id)sender
{

    if(marioRunning.selected)
    {
        [UIView animateWithDuration:0.50 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            marioRunning.center = CGPointMake(160.0, -100.0);
        } completion:^(BOOL finished) {
            if (finished) {
                [UIView animateWithDuration:0.00 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                    marioRunning.center = CGPointMake(-30.5, 456.0);
                } completion:^(BOOL finished) {
                    if (finished) {
                        [UIView animateWithDuration:1.50 delay:2.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                            marioRunning.center = CGPointMake(160.0, 456.0);
                        } completion:^(BOOL finished) {
                            if(finished)  // NSLog ( @"Finished !!!!!" );
                                marioRunning.center = CGPointMake(160.0, 456.0);
                        }];
                    }
                }];
            }
        }];

        // Mario Jump Sound

        SystemSoundID soundID;
        NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"Super_Mario_Bros_Jump" ofType:@"mp3"];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile], & soundID);
        AudioServicesPlaySystemSound(soundID);

        // Mario Running Animation

        marioRunning.imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"mario-running2"],[UIImage imageNamed:@"mario-running3"],nil];

        marioRunning.imageView.animationDuration = 0.2;
        marioRunning.imageView.animationRepeatCount = 20;

        [marioRunning.imageView startAnimating];
    }

    else

    {
        [UIView animateWithDuration:1.50 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            marioRunning.center = CGPointMake(350.5, 456.0);
        } completion:^(BOOL finished) {
            if (finished) {
                [UIView animateWithDuration:0.00 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                    marioRunning.center = CGPointMake(-30.5, 456.0);
                } completion:^(BOOL finished) {
                    if (finished) {
                        [UIView animateWithDuration:1.50 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                            marioRunning.center = CGPointMake(160.0, 456.0);
                        } completion:^(BOOL finished) {
                            if(finished)  // NSLog ( @"Finished !!!!!" );
                                marioRunning.center = CGPointMake(160.0, 456.0);
                        }];
                    }
                }];
            }
        }];

        marioRunning.imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"mario-running2"],[UIImage imageNamed:@"mario-running3"],nil];

        marioRunning.imageView.animationDuration = 0.15;
        marioRunning.imageView.animationRepeatCount = 19;

        [marioRunning.imageView startAnimating];
    }

    marioRunning.selected = !marioRunning.selected;

}

- (IBAction)bowserJump_clicked:(id)sender
{

    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        bowserJump.center = CGPointMake(250.0f, 100.0);
    } completion:^(BOOL finished) {
        if(finished) {
            [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                bowserJump.center = CGPointMake(250.0, 436.5);
            } completion:^(BOOL finished) {
                if(finished)  // NSLog ( @"Finished !!!!!" );
                    bowserJump.center = CGPointMake(250.0, 436.5);
            }];
        }
    }];

    SystemSoundID soundID;
    NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"smb_bowserfalls" ofType:@"wav"];

    AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile], & soundID);
    AudioServicesPlaySystemSound(soundID);

}




-(void) touchesBegan:(NSSet*) touches withEvent:(UIEvent *) event {

    CGPoint point = [[touches anyObject] locationInView:self.view];

    if([self.cloudAnimate.layer.presentationLayer hitTest:point]) {

        SystemSoundID soundID;
        NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"Super_Mario_Bros_Mushroom" ofType:@"mp3"];

        AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile], & soundID);
        AudioServicesPlaySystemSound(soundID);

        cloudAnimate.imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"panda-png-chewing"],[UIImage imageNamed:@"panda-png-happy"],nil];

        cloudAnimate.imageView.animationDuration = 0.2;
        cloudAnimate.imageView.animationRepeatCount = 6;
        [cloudAnimate.imageView startAnimating];
    }
    else if([self.cloudAnimate1.layer.presentationLayer hitTest:point]) {

        SystemSoundID soundID;
        NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"Super_Mario_Bros_Mushroom" ofType:@"mp3"];

        AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile], & soundID);
        AudioServicesPlaySystemSound(soundID);

        cloudAnimate1.imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"panda-png-chewing"],[UIImage imageNamed:@"panda-png-happy"],nil];

        cloudAnimate1.imageView.animationDuration = 0.2;
        cloudAnimate1.imageView.animationRepeatCount = 6;
        [cloudAnimate1.imageView startAnimating];
    }

}

@end

如果有人可以提供帮助,请提前致谢。

【问题讨论】:

  • 您真的有 4 个不同的故事板(4 个不同的文件,扩展名为 .storyboard),还是您在一个故事板中谈论不同的场景(控制器)?另外,你在 map2 上的哪个动画有错误的动画时间?
  • 是的,抱歉,1 个故事板和 4 个视图控制器——我的错。现在将编辑并为第二个视图控制器添加动画。
  • 我不明白为什么会发生这种情况。我要改变的一件事,无论这是否解决了问题,就是使用基于块的方法来制作 openMenu 动画,就像对所有其他动画所做的那样。
  • 为了澄清,你说如果你在 mapOne 上调用 OpenMenu(我应该是 'openMenu'),转到 mapTwo,那里的每个动画都以 0.3 的速度进行动画?
  • @random - 是的,这正是我要说的。如果我在启动后首先进入地图二,动画以在 mapTwo.m 中设置的速度运行,如果我进入 mapOne,然后打开 openMenu 并返回到 mapTwo,所有动画都以 0.3 秒运行!呸呸呸

标签: ios objective-c animation


【解决方案1】:

@rdelmar,这应该归功于你,因为我刚刚尝试了你所说的并且它奏效了。

我更改了动画的结构以匹配我已经用于其他动画的结构,它完成了这项工作,非常感谢。

我将它用于 mapOne 文件,现在一切正常。

- (IBAction)openMenu_clicked:(id)sender {
    if (draw1 == 0) {
        draw1 = 1;           
        [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            scrollView.frame = CGRectMake(0, 468, 320, 100);
        } completion:^(BOOL finished) {
        }];

        [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            openMenu.frame = CGRectMake(124, 396, 72, 72);
        } completion:^(BOOL finished) {   
        }];

    } else {
        draw1 = 0;
        [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            scrollView.frame = CGRectMake(0, 568, 320, 0);
        } completion:^(BOOL finished) {

        }];

        [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            openMenu.frame = CGRectMake(124, 498, 72, 72);
        } completion:^(BOOL finished) {

        }];
    }
}

【讨论】:

    猜你喜欢
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    相关资源
    最近更新 更多