0

这可能很疯狂,但我想知道我们是否可以在选择器中编写实际方法。原因是,我的方法只有一个简单的 1-liner。

-(void)doThat {
    loadedPhoto.alpha = 1.0;
}

-(IBAction)revealPickers:(id)sender {

    loadedPhoto.alpha = 0.7;
    [self performSelector:@selector(doThat) withObject:nil afterDelay:1.0];
}

所以我想知道是否有这样的捷径(显然这行不通):

[self performSelector:@selector(^(void){loadedPhoto.alpha=1.0;}) withObject:nil afterDelay:1.0];

可能还是只是愚蠢?

4

2 回答 2

1

扩展和更正我的评论:

我的评论是错误的,我误读了您的代码并认为您在做[self performSelectorOnMainThread:etc:].

我的实际解决方案(经过快速谷歌搜索 - 并不难找到):

// Delay execution of my block for 10 seconds.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_current_queue(), ^(void){
    loadedPhoto.alpha = 1.0;
});

希望能帮助到你!

于 2013-03-29T11:40:17.357 回答
0

最简单的解决方案是使用dispatch_after,但您也可以定义NSObject使用块适配器的方法。

@interface BlockAdapter

@property (nonatomic, copy, readwrite) void (^block)();

- (void)call;

@end

@implementation BlockAdapter

- (void)call {
    self.block();
}

@end
@interface NSObject (PerformBlocks)

- (void)performBlock:((void)^())block afterDelay:(NSTimeInterval)delay;

@end

@implementation NSObject (PerformBlocks)

- (void)performBlock:((void)^())block afterDelay:(NSTimeInterval)delay {
   BlockAdapter* adapter = [[BlockAdapter alloc] init];
   adapter.block = block;

   [adapter performSelector:@selector(call) withObject:nil afterDelay:delay];
}

@end
于 2013-03-29T11:51:03.590 回答