4

KVOからこのメッセージを呼び出そうとしています。画像がダウンロードされると、このメッセージが送信されます。補完ブロックのメッセージには、正常に動作するアニメーションも含まれています(正しくアニメートされます)。このアニメーションは、アニメーションが発生しない状態で変換を適用します(アニメーションの長さを待ってから、最終状態にジャンプします)。UIView animateWithDuration:アニメーション:補完:変換を適用し、アニメーション化しない

/** 
* Discover the subview with the supplied tag, attach the fullsize image to the view 
* scale to fullsize and begin retract. 
* @param viewTag int - #FUTURE USE# - The tag of the view to be animated. 
* @param image UIImage - #FUTURE USE# - The image to be applied to the view. 
* @return void 
*/ 
- (void)animateViewWithTag:(int)viewTag andImage:(UIImage *)image { 

    Panel *activePanel = [self.panels objectAtIndex:currentIndex]; 
    UIView *activePanelView = [self.view viewWithTag:activePanel.panelId]; 

    // Display the transition to the fullsize version of the panel image. 
    // Determine the scale that needs to be applied to the view to show 
    // the image in the appropriate scaling. If scaled image is greater than 
    // the size of the screen, find the best fit. 

    float scale = image.size.width/activePanelView.frame.size.width; 

    if (image.size.width > self.view.window.frame.size.width || image.size.height > self.view.window.frame.size.height) { 
     // The image will scale beyond the bounds of the window, scale must be adjusted. 
     scale = self.view.window.frame.size.width/activePanelView.frame.size.width; 
    } 

    CGAffineTransform transform = CGAffineTransformMakeScale(scale, scale); 

    [UIView animateWithDuration:1.0 
        animations:^{ 
         // Get the fullsize image and display it in the growing panel. 
         [activePanelView setTransform:transform]; 
         [NSThread sleepForTimeInterval:3.0]; 
        } 
        completion:^(BOOL finished) { 
         [self retractImage:activePanelView]; 
        }]; 
} 


#pragma mark - KVO 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {  
    int tmpInt = (int)context;   
    UIImage *tmpImage = [change objectForKey:NSKeyValueChangeNewKey]; 

    if (keyPath == @"imgOriginal") { 
     [self animateViewWithTag:[(Panel *)object panelId] andImage:tmpImage];    
    } 

} 
+0

メインスレッドでこのメソッドを呼び出してみましたか? – NeverBe

+0

私は[self animateViewWithTag:]をラップしましたので、dispatch_async(dispatch_get_main_queue()、^ {[animate ....]と同じ効果がありました) – Kyle

+0

なぜ必要な 'viewTag'を渡しているのでしょうか?それが役に立てば幸い。 –

答えて

1

スレッドスリープの目的は何ですか?

メインスレッドをスリープ状態にすると、その間にアニメーションは更新されません。

メインスレッドでこれを呼び出さない場合、UIKitアニメーションはスレッドセーフではなく、メインスレッドからのみ確実に使用できるため、動作しません。

+0

睡眠は単に最終状態でアニメーションを遅らせることです。あなたの投稿から、それはアニメーション全体を通して寝ているように思われます。 – Kyle

+0

はい。アニメーションコードはアニメーションの終わりには起動しません。アニメーションコードは最初から起動されますが、値を直接設定するのではなくCATransaction内にあるため、時間の経過と共に値を補間します。アニメーションの後にインターフェイスを3秒間フリーズすることで達成しようとしていることは本当にわかりません(メインスレッドをスリープさせてもユーザーとのやりとりはできません)が、完了ブロックでスティックする方が良いでしょうアニメーションブロックの代わりに(私はどちらも動作するかどうかはわかりません)。 –

+0

アニメーションが終了してから3秒後まで何かを遅延させたい場合は、performSelector:withObject:afterDelay:またはdispatch_after(...)を使用してブロックを使用することをお勧めします。 –

関連する問題