我正在创建一个带有默认图像的 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]);
}
}];
谢谢
最佳答案
我在 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
您只需要一个占位符图像,这样动画就可以在第一时间出现。
关于ios - 是否可以在 PFImageView 中为新加载的图像设置动画?与 parse.com 相关的 PFImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15979084/