[英]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.