2013-04-12 24 views
0

我創建一個PFImageView與默認圖像。然後我在後臺加載一個圖像,當圖像從服務器上完成下載時,它立即顯示出來,佔位符/默認圖像被替換。問題是,從默認轉換到下載的圖像不是很愉快。有沒有一種方法可以創建淡入淡出狀態並淡入到下載的動畫中?是否可以在PFImageView中爲新加載的圖像設置動畫效果?相關parse.com PFImageView

這是我的代碼。

// set the pfImageView into the webView 
_pfImageView = [[PFImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%dpTall.jpg", [self.drink.glassId intValue]]]]; 
_pfImageView.frame = CGRectMake(244, 0, 76, 114); 
[webView.scrollView addSubview:_pfImageView]; 

// create a query for finding images on Parse 
PFQuery *query = [PFQuery queryWithClassName:@"Images"]; 
[query whereKey:@"drinkId" equalTo:[NSNumber numberWithInt:self.drink.primaryKey]]; 

    [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) { 

    if (!error) { 
     PFFile *imageFile = [object objectForKey:@"image"]; 
     _pfImageView.file = imageFile; 
     [_pfImageView loadInBackground]; 

    } else { 
     NSLog(@"PFQuery Error:%@", [error localizedDescription]); 
    } 
}]; 

感謝

+0

你在尋找什麼樣的過渡? –

+0

UIViewAnimationOptionTransitionCrossDissolve – jmurphy

回答

0

在我看來,我認爲它會更容易砸PFImageView的依賴,只是使用普通的UIImageView,並設置有佔位第一,像你現在正在做。然後,在IF語句您getFirstObjectInBackgroundWithBlock完成塊始做這樣的事情....

if(!error) 
{ 
    PFFile *imageFile = [object objectForKey:@"image"]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData *data = [imageFile getData]; 

     if(data) 
     { 
      dispatch_async(dispatch_get_main_queue(), ^{ 

       [UIView animateWithDuration:2.0 
       animations:^{ 
        //imageView is your UIImageView you are trying to set/change 
        imageView.alpha = 0.0f; 
       } 
       completion:^(BOOL finished){ 
        [UIView animateWithDuration:2.0 
        animations:^{ 
         imageView.image = [UIImage imageWithData:data]; 
         imageView.alpha = 1.0f; 
        } 
        completion:^(BOOL finished) 
        { 
        }]; 
       }];//outside block 
      }); 
     } 
     else 
     { 
      //no data error handling here 
     }     

    }); 
else 
{ 
    //error handling here 
} 

披露:我不是附近的一個編譯器,我沒有運行此代碼呢。

你也可以使用imageFile getdata方法調用一個完成塊(它在parse.com網站API上),然後把if(data)放在該塊的所有內容中,而不是使用dispatch_async我做的異步塊。我習慣這樣做。

+0

你絕對正確。我只是建議解析他們爲他們的PFImageView類添加了一個新的屬性,允許我們設置一個transition屬性。但在此之前,您的解決方案將工作得很好。 謝謝! – jmurphy

3

我對PFImageView類別各出現圖像時獲得的淡入淡出動畫:

@implementation PFImageView (Utility) 

// Add a fade animation to images 
-(void) setImage:(UIImage *)image { 
    if ([self.layer animationForKey:KEY_FADE_ANIMATION] == nil) { 
     CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"]; 
     crossFade.duration = 0.6; 
     crossFade.fromValue = (__bridge id)(self.image.CGImage); 
     crossFade.toValue = (__bridge id)(image.CGImage); 
     crossFade.removedOnCompletion = NO; 
     [self.layer addAnimation:crossFade forKey:KEY_FADE_ANIMATION]; 
    } 

    [super setImage:image]; 
} 

@end 

你只需要一個佔位符圖像,動畫可以發生在第一時間。

相關問題