gpt4 book ai didi

objective-c - 在 iOS 上使用多个 NSTimer - 只有一个计时器触发

转载 作者:行者123 更新时间:2023-12-01 18:33:13 25 4
gpt4 key购买 nike

需要帮助解决问题。

目标
我正在整理一个 iOS 图书应用程序,它使用 NSTimers 在加载 View 后触发几个交错的动画事件。我创建了一个 MethodCallerWithTimer 类来帮助我做到这一点(代码在底部)。

到目前为止我的解决方案
当我使用 MethodCallerWithTimer 类时,我将 objectOwningMethod 分配为我的 UIViewController 子类对象(它是一个书页),然后将该方法作为该类中的实例方法。这是我指定的方法的示例 - 非常简单地打开屏幕上的一些艺术品:

- (void) playEmory {
[emoryRedArt setHidden:NO];
}

我的问题
当我创建多个 MethodCallerWithTimer 实例然后加载 View 并启动它们时,我只会让 FIRST 事件发生。其他计时器都没有调用它们的目标方法。我怀疑我不明白我要求 NSRunLoop 做什么或类似的事情。

有什么想法吗?

这是我的 MethodCallerWithTimer 类:
@interface MethodCallerWithTimer : NSObject {
NSTimer * timer;
NSInvocation * methodInvocationObject;
NSNumber * timeLengthInMS;
}

- (id) initWithObject: (id) objectOwningMethod AndMethodToCall: (SEL) method;
- (void) setTime: (int) milliseconds;
- (void) startTimer;
- (void) cancelTimer;

@end

和实现:
#import "MethodCallerWithTimer.h"

@implementation MethodCallerWithTimer

- (id) initWithObject: (id) objectOwningMethod AndMethodToCall: (SEL) method {
NSMethodSignature * methSig = [[objectOwningMethod class] instanceMethodSignatureForSelector:method];
methodInvocationObject = [NSInvocation invocationWithMethodSignature:methSig];
[methodInvocationObject setTarget:objectOwningMethod];
[methodInvocationObject setSelector:method];
[methSig release];
return [super init];
}
- (void) setTime: (int) milliseconds {
timeLengthInMS = [[NSNumber alloc] initWithInt:milliseconds];
}
- (void) startTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:([timeLengthInMS longValue]*0.001) invocation:methodInvocationObject repeats:NO];
}
- (void) cancelTimer {
[timer invalidate];
}
-(void) dealloc {
[timer release];
[methodInvocationObject release];
[timeLengthInMS release];
[super dealloc];
}

@end

最佳答案

这些看起来像是延迟后的一次性发射;你有没有考虑过使用类似的东西:

[myObject performSelector:@selector(playEmory) withObject:nil afterDelay:myDelay];

在哪里 myObjectplayEmory 的实例常规和 myDelayfloat您希望操作系统在调用电话之前等待的秒数?

您可以找到更多关于 performSelector flavor 的信息。 here .

关于objective-c - 在 iOS 上使用多个 NSTimer - 只有一个计时器触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5518675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com