为什么我的一个NSTimer弄乱了我的背景动作?

时间:2014-09-09 17:21:10

标签: ios ios7 nstimer

我有两个NSTimer。问题是我的倒数计时器在倒数计时器停止后会立即弄乱我的背景定位。背景正常移动,直到后台计时器停止。然后它会自动将背景重新定位到起始位置,这是我不想要的,因为它发生时似乎很小。倒数计时器停止后,我希望它能够顺畅地流动。如果我把倒数计时器拿出来,背景运动就完美了。这是我的代码:

PlayViewController.h

#import <UIKit/UIKit.h>

@interface PlayViewController : UIViewController
{
    int countdownStatus;
    IBOutlet UIImageView *background;
    NSTimer *backgroundMovement;
    NSTimer *gameCountdownTimer;
}

@property (strong, nonatomic) IBOutlet UILabel *countdownLabel;

- (void)backgroundMoving;
- (void)gameCountdown;

@end

PlayViewController.m

#import "PlayViewController.h"

@interface PlayViewController ()

@end

@implementation PlayViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.countdownLabel.text = @"3";
    countdownStatus = 2;
    gameCountdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(gameCountdown) userInfo:nil repeats:YES];
    backgroundMovement = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(backgroundMoving) userInfo:nil repeats:YES];
}

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

- (void)backgroundMoving
{
    background.center = CGPointMake(background.center.x - 1, background.center.y);
    CGRect backgroundFrame = background.frame;

    if(backgroundFrame.origin.x < -370)
    {
        CGRect myFrame = background.frame;
        myFrame.origin.x = 0;
        myFrame.origin.y = 0;
        background.frame = myFrame;
    }
}

- (void)gameCountdown
{
    if(countdownStatus == 2)
        self.countdownLabel.text = @"2";

    else if(countdownStatus == 1)
        self.countdownLabel.text = @"1";

    else
        self.countdownLabel.text = @"";

    countdownStatus--;
}

@end

0 个答案:

没有答案