最初のメソッドは 2 番目のアニメーションを作成し、2 番目のメソッドはいくつかのタスクを実行します。
このクラスは、これら 2 つの操作を連続して実行するために 2 番目のクラスから呼び出されていますが、最初の操作が終了したときにのみ 2 番目の操作が実行されるようにロックを適用したいと考えています。
私の質問は、それを行う最善の方法は何ですか。
ここに私のコードがあります:
@implementation Server
- (id)init{
if ( (self = [super init]) ) {
syncLock = [[NSLock alloc] init];
}
return self;
}
- (void)operationA {
NSLog(@"op A started");
[syncLock lock];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
[view setBackgroundColor:[UIColor redColor]];
[[[[UIApplication sharedApplication] delegate] window] addSubview:view];
[UIView beginAnimations:@"opA" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationFinished)];
[UIView setAnimationDuration:1.5f];
[view setFrame:CGRectMake(50, 50, 150, 150)];
[UIView commitAnimations];
}
- (void)animationFinished {
[syncLock unlock];
NSLog(@"Op A finished");
}
- (void)operationB {
if ( ![syncLock tryLock]) {
[[NSRunLoop currentRunLoop] addTimer:[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(operationB) userInfo:nil repeats:NO] forMode:NSDefaultRunLoopMode];
return;
}
NSLog(@"op B started");
NSLog(@"perform some task here");
[syncLock unlock];
NSLog(@"op B finished");
}
@end
そして、それを呼び出すコード:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
Server *server = [[Server alloc] init];
[server operationA];
[server operationB];
return YES;
}