如何在用户触摸屏幕后延迟代码运行

时间:2014-06-12 01:44:07

标签: ios objective-c

我正在制作Tap游戏,点击屏幕开始游戏。 我有这个代码,我需要在点击屏幕后启动这个动画。现在正在加载游戏时运行(不点击屏幕)。我需要更改它以在屏幕上的用户选项卡(触摸)后启动此动画?谢谢你的帮助。

[super viewDidLoad];

// Set Delay on Animations when Game Start - Animations Area ***** PERFORMS ****** 0.1
[self performSelector:@selector(Animation) withObject:nil afterDelay:0.1];

2 个答案:

答案 0 :(得分:1)

在视图中添加点击手势识别器,然后在选择器中启动动画。

-(void) viewDidLoad
{
     [super viewDidLoad];


     UITapGestureRecognizer *tapGestureRg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(beginAnimation)];
    [self.view addGestureRecognizer:tapGestureRg];

}


   -(void) beginAnimation
{
    // Set Delay on Animations when Game Start - Animations Area ***** PERFORMS ****** 0.1
    [self performSelector:@selector(Animation) withObject:nil afterDelay:0.1];
}

如果您想在触摸时开始动画,则覆盖-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event并在那里开始动画。

答案 1 :(得分:0)

你可以添加Tap Gesture到屏幕。如果你有UIViewController并想要点击UIViewController视图点击开始动画,那么你可以使用这个代码,或者你可以改变你想要点击并开始的视图动画

-(void) viewDidLoad
{
     [super viewDidLoad];

    UITapGestureRecognizer *singleFingerTap = 
      [[UITapGestureRecognizer alloc] initWithTarget:self 
                                              action:@selector(handleSingleTap:)];
    [self.view addGestureRecognizer:singleFingerTap];  //Replace the self.view if you want to add single tap on another view with your view refrence
 }

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {

   [self Animation]; //You should rename this method as animation.Follow naming conventions.
}