基于“点击”如何访问计时器对象并停止处理
是的,另一个新手问题 - 流程/工作流程问题。请耐心等待。
我有一个 NSTimer 对象,计时器,在一个方法 methA 中创建,被传递到另一个方法 methB,用于控制某些处理。两种方法都在同一个类中。
我有touchesBegin 和touchesEnded 方法来捕获用户输入。这些方法与我之前的两个方法(methA 和 methB)属于同一类。当用户在我的屏幕上“点击”时,我需要停止处理
当我的 TouchBegin 方法被点击调用时,我假设我所要做的就是向我的其他方法 methA/methB 发送一条消息,并告诉它们停止加工。我假设我所要做的就是使传递给我的“处理”方法(即 methB)的计时器无效。
这听起来正确吗?我已经包含了四种方法:touchesBegin、methA 和 methB。任何意见都将受到高度赞赏。
- (void) methA
{
stepValue = 0;
animationBuild = YES;
float duration = [[[Config shared] valueForKey:@"animation.val.step_duration"] floatValue];
[NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(stepValue:) userInfo:nil repeats:YES];
}
- (void) methB:(NSTimer *) timer
{
if (animationBuild)
{
// animation logic/processing
}
// Next step
stepValue++;
if (stepValue == GROUP_SIZE)
{
[timer invalidate];
[self animateShowMessage];
}
}
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
if (modalDialog)
{
return;
}
if (currentTouch == nil)
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
currentTouch = [touch retain];
}
}
- (void) touchesEnded:(NSSet *) touches withEvent:(UIEvent *) event
{
if (modalDialog)
{
return;
}
UITouch *touch = [[touches allObjects] objectAtIndex:0];
if ((touch != nil) && (touch == currentTouch))
{
CGPoint touchPoint = [touch locationInView:self.view];
else if ((CGRectContainsPoint(visRect[[Process shared].procType], point)) && (touch.tapCount == 2))
{
// processing
}
else
{
// Start a new processing
[self startNew];
}
[currentTouch release];
currentTouch = nil;
}
}
Yes, another newbie question - process/workflow question. Please be patient.
I've got a NSTimer object, timer, created in one method methA, being passed to another method methB where it's used to contorl some processing. Both methods are within the same class.
I have touchesBegin and touchesEnded methods to capture user input. These methods are in the same class as my two previous methods - methA and methB. When a user "taps" on my screen I need to stop processing
When my touchesBegin method is invoked by a tap I'm assuming all I have to do is send a message to my other methods, methA/methB, and tell them to stop processing. I'm assuming all I have to do is invalidate the timer that's being passed to my "processing" method, i.e. methB.
Does this sound right? I've included my four methods touchesBegin, methA, and methB. Any input is greatly appreciated.
- (void) methA
{
stepValue = 0;
animationBuild = YES;
float duration = [[[Config shared] valueForKey:@"animation.val.step_duration"] floatValue];
[NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(stepValue:) userInfo:nil repeats:YES];
}
- (void) methB:(NSTimer *) timer
{
if (animationBuild)
{
// animation logic/processing
}
// Next step
stepValue++;
if (stepValue == GROUP_SIZE)
{
[timer invalidate];
[self animateShowMessage];
}
}
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
if (modalDialog)
{
return;
}
if (currentTouch == nil)
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
currentTouch = [touch retain];
}
}
- (void) touchesEnded:(NSSet *) touches withEvent:(UIEvent *) event
{
if (modalDialog)
{
return;
}
UITouch *touch = [[touches allObjects] objectAtIndex:0];
if ((touch != nil) && (touch == currentTouch))
{
CGPoint touchPoint = [touch locationInView:self.view];
else if ((CGRectContainsPoint(visRect[[Process shared].procType], point)) && (touch.tapCount == 2))
{
// processing
}
else
{
// Start a new processing
[self startNew];
}
[currentTouch release];
currentTouch = nil;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它本质上是正确的,但是,我会进行一些设计更改。
除非您正在构建 iOS 3.x 之前的应用程序,否则您应该使用 UIGestureRecognizer 来识别点击,而不是旧的 TouchBegan 方法。使用识别器非常简单:
我通常不喜欢在代码中多处修改计时器。如果您的计时器修改分散在整个代码中,您可能会错过某处的失效或释放。因此,我通常创建一个计时器属性,如下例所示。如果您使用这样的属性,更改计时器对象或将计时器设置为零会自动使其失效,因此保证它永远不会处于时髦状态。
It's essentially correct, however, I would make a couple of design changes.
Unless you are building for pre-iOS 3.x app, you should be using a
UIGestureRecognizer
to recognize taps, not the older touchesBegan approach. Using a recognizer is very easy:I typically don't like to have more than one place in my code that I modify a timer. If you have timer modification scattered throughout code, you are likely to miss an invalidation or release somewhere. So, I usually create a timer property like the example below. If you use a property like this, changing your timer object or setting your timer to nil will automatically invalidate it, so its guaranteed to never be in a funky state.