简体   繁体   English

如何启动和关闭计时器?

[英]How to activate and deactivate a timer?

So i am trying to create a fixed movement for an image, but i can't seem to make it work correctly. 因此,我正在尝试为图像创建固定的运动,但是我似乎无法使其正常工作。 The first block is activate by a timer but I don't know how to turn it off and activate another timer to continue the curve. 第一个块是由计时器激活的,但是我不知道如何关闭它并激活另一个计时器以继续曲线。

    BMR = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(BallArchR) userInfo:nil repeats:YES];
    LMR = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(BallArchL) userInfo:nil repeats:YES];


    -(void)BallArchR{
        if (Ball.center.x < 284){
            Ball.center = CGPointMake(Ball.center.x + 2.72 , Ball.center.y - 2 );
        }
        if (Ball.center.x >= 284 && Ball.center.x < 488) {
    Ball.center = CGPointMake(Ball.center.x + 2.72 , Ball.center.y + 2 );
        }
    }

    -(void)BallArchL{
        if (Ball.center.x > 284){
            Ball.center = CGPointMake(Ball.center.x - 50 , Ball.center.y - 50 );
        }
        if (Ball.center.x <= 284 && Ball.center.x > 80) {
            Ball.center = CGPointMake(Ball.center.x - 2.72 , Ball.center.y + 2 );
        }
    }

This is the code to make the ball move in a curve, and BallArchR is activated when the game starts up. 这是使球沿曲线移动的代码,并且在游戏开始时激活BallArchR。 But i can't turn it off and activate BallArchL. 但是我无法关闭它并激活BallArchL。 Is there another way to make this work? 还有另一种方法可以使这项工作吗?

- (void)stopTimer 
{
    if ([_LMR isValid]) 
{
        [_LMR invalidate];
    }
}

You can use this for stop timer. 您可以将其用作停止计时器。

Also for animation you can use 也可以使用动画

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
//move your view
[UIView commitAnimations];

or 要么

[UIView animateWithDuration:duration
                            animations:^{
                            //move your view
                            }
];

i think one timer is enough for this 我认为一个计时器就足够了

 BMR = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(BallArchR) userInfo:nil repeats:YES];

-(void)BallArchR
 {
        if (Ball.center.x < 284)
        {
            Ball.center = CGPointMake(Ball.center.x + 2.72 , Ball.center.y - 2 );
        }
        if (Ball.center.x >= 284 && Ball.center.x < 488) 
        {
            Ball.center = CGPointMake(Ball.center.x + 2.72 , Ball.center.y + 2 );
        }
        else
        {
             [self BallArchL ];
        }
  }
  -(void)BallArchL
  {
        if (Ball.center.x > 284)
        {
            Ball.center = CGPointMake(Ball.center.x - 50 , Ball.center.y - 50 );
        }
        if (Ball.center.x <= 284 && Ball.center.x > 80)
        {
            Ball.center = CGPointMake(Ball.center.x - 2.72 , Ball.center.y + 2 );
        }
        else
        {
            [BMR invalidate];
        }
  }

i'm answered this based on what i have understand,if it not what you want comment here your requirement clearly...;) 我根据我的理解回答了这个问题,如果不是您想要在这里发表评论,请明确说明您的要求...;)

When you need to deactivate timer, first check if the timer is valid or not then deactivate by using "invalidate" method. 当需要停用计时器时,请先检查计时器是否有效,然后使用“无效”方法停用计时器。

 if ([BMR isValid]) 
{
    [BMR invalidate];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 停用并激活特定的UILocalNotification - deactivate and activate a specific UILocalNotification 激活/停用代码 - Activate/deactivate code 激活/停用自动布局NSLayoutConstraints - Activate/deactivate autolayout NSLayoutConstraints iOS如何为特定大小的类别(方向)激活/停用布局约束? - iOS how to activate/deactivate layout constraints for a certain size class (orientation)? 什么时候可以激活/停用布局约束? - When can I activate/deactivate layout constraints? 是否可以在单个ViewController中激活和停用Bump API? - Is it possible to activate & deactivate bump api within a single viewcontroller? iPad多任务根据情节提要中的自动布局激活/停用约束 - iPad multitasking activate/deactivate constraints according to autolayout in storyboard UIswitch在单独的视图控制器中激活或停用图像阵列 - UIswitch to activate or deactivate an image array in seperate view controller 使用 startUpdatingLocation() 和 stopUpdatingLocation() 在特定视图中激活/停用位置更新 - Using startUpdatingLocation() and stopUpdatingLocation() to activate/deactivate location updates in specific Views 如何停用 WatchConnectivity session? - How to deactivate the WatchConnectivity session?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM