动画速度继承自第一个View Controller

时间:2013-12-13 21:08:50

标签: ios objective-c animation

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

所有3个都按照我的意愿完美地工作,所有人都在触摸时做了不同的动画,所以一切都很好。

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

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

仅当我实际使用mapOne视图控制器上的pop菜单时才会出现这种情况。

所以,如果我首先去mapTwo视图控制器,它应该工作。

如果我首先去mapOne视图控制器并且不使用弹出菜单并返回mapTwo视图控制器那么动画就可以正常工作。

但是如果我首先去mapOne视图控制器然后点击菜单中的弹出菜单然后返回mapTwo控制器所有动画都在0.3秒内动画,弹出菜单在mapOne视图控制器上动画的速度。

任何人都知道这可能是什么?

以下是我的mapOne视图控制器的代码,其中包含pop菜单中的代码:

#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 View控制器位于:

#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

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

1 个答案:

答案 0 :(得分:0)

@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) {

        }];
    }
}