ユーザーは、スワイプジェスチャでアニメーションを開始できます。アニメーションへの重複呼び出しをブロックして、アニメーションが開始されると、完了するまで再開できないようにします。これは、ユーザーが誤って複数回スワイプした場合に発生する可能性があります。
ほとんどの人BOOL isAnimatingFlag
は、下に示すようにブールフラグ()を使用してこの制御を実現していると思います。私はこれまでアプリでこのようなことを何度も行ってきましたが、アニメーションはブロックを使用しており、アニメーションの完成がどのスレッドであるかが不明なため、フラグが意図した値を確実に持つかどうかについて100%確信することはありません。ブロックが実行されています。
この方法(重複するアニメーションをブロックする方法)は、マルチスレッド実行に対して信頼できますか?
/* 'atomic' doesn't
* guarantee thread safety
* I've set up my flag as follows:
* Does this look correct for the intended usage?
*/
@property (assign, nonatomic) BOOL IsAnimatingFlag;
//…
@synthesize IsAnimatingFlag
//…
-(void)startTheAnimation{
// (1) return if IsAnimatingFlag is true
if(self.IsAnimatingFlag == YES)return;
/* (2) set IsAnimatingFlag to true
* my intention is to prevent duplicate animations
* which may be caused by an unwanted double-tap
*/
self.etiIsAnimating = YES;
// (3) start a new animation
[UIView animateWithDuration:0.75 delay:0.0 options:nil animations:^{
// animations would happen here...
} completion:^(BOOL finished) {
// (4) reset the flag to enable further animations
self.IsAnimatingFlag = NO;
}];
}