2012-03-19 134 views
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 2012-03-19 19:55:04

+0

我用[dispatch_get_main_queue(),^ {[animate ....)包裝了[self animateViewWithTag:]像這樣效果。 – Kyle 2012-03-19 20:13:05

+0

我想知道,爲什麼你傳遞'required'' viewTag'而不是使用它:)希望能幫助到你。 – 2012-03-19 22:36:21

回答

1

線程睡眠的目的是什麼?

如果你讓主線程睡眠,那麼它不會在此期間更新動畫。

如果你不在主線程上調用它,那麼它也不會工作,因爲UIKit動畫不是線程安全的,只能從主線程可靠地使用。

+0

睡眠只是爲了在最終狀態下延遲動畫。從你的帖子看,儘管它似乎可能正在整個動畫中沉睡。 – Kyle 2012-03-20 11:35:15

+0

是的。動畫代碼不會在動畫結束時觸發,它會在開始時觸發,但由於它位於CATransaction中,而不是直接設置值,所以隨着時間的推移,它們會在它們之間進行插值。我不確定你想要通過在動畫之後凍結界面3秒達到什麼目的(如果你睡覺主線程,不會有用戶交互),但是最好將它粘在完成塊中而不是動畫塊(我不知道這肯定會起作用)。 – 2012-03-20 13:57:17

+0

如果你只是想延遲發生的動作,直到動畫完成後3秒,我建議使用performSelector:withObject:afterDelay:或dispatch_after(...)如果你喜歡使用塊 – 2012-03-20 14:00:16