簡體   English   中英

隱藏某件事一定時間后如何做出if語句

[英]How to make a if statement over after a certain thing has been hidden for a certain time

我正在做一個游戲,我需要知道是否可以制作一個if語句,使其像這樣:

if object.hidden = YES for 5 seconds{ 
    do these things
}

有人可以告訴我這是否可行,如果可以的話如何運作?

在對象隱藏時為其添加時間戳記。 然后將測試轉換為“該對象已隱藏多長時間”。 偽代碼(因為我實際上沒有使用目標C的經驗):

if hidden and elapsed time since hidden > 5 seconds:
    do stuff

您可以使用CACurrentMediaTime()獲得正確的時間間隔。

樣品:

CFTimeInterval start = CACurrentMediaTime(); 

for(int i=0; i<=10000; i++) {
    NSLog(@"%i", i);
} 

CFTimeInterval elapsed = CACurrentMediaTime() - start;

NSLog(@"elapsedTime : %f", elapsed);

確保已在您的Traget設置中添加了QuartzCore框架

您可以將Object子類化並構建該功能。類似:

@interface ObjectSubclass : Object

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ObjectSubclass

-(void)setHidden:(BOOL)hidden{

    [super setHidden:hidden];

    if (hidden){

        if (self.timer) {
            [self.timer invalidate];
        }

        self.timer = [NSTimer timerWithTimeInterval:5
                                             target:self
                                           selector:@selector(doStuff:)
                                           userInfo:nil
                                            repeats:NO];

        [[NSRunLoop currentRunLoop] addTimer:self.timer
                                     forMode:NSDefaultRunLoopMode];

    } else {

        [self.timer invalidate];
        self.timer = nil;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM