ios - 是否可以在 PFImageView 中为新加载的图像设置动画?与 parse.com 相关的 PFImageView

标签 ios animation uiimageview parse-platform

我正在创建一个带有默认图像的 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/

相关文章:

ios - Swift: NSArrayM controllerWillChangeContent:]: 无法识别的选择器发送到实例

ios - 用户通过swift在Apple In app purchase上购买某些商品时如何获取一些用户信息?

objective-c - 这个 Xcode 警告是什么意思? "application executable is missing required architecture. ..."

python - 如何使用 Mayavi 在 python 中制作 3D 数据动画

cocoa-touch - 放置大图像时 UIImageView 图像失真

ios - iPhone iOS Amazon Scheme/URI 不再工作 amzn ://version 4. 3.1 深层链接

javascript - jQuery 在 CSS 中重新定位之前查找元素位置

javascript - 带有焦点的输入示例打破表单的向下滑动动画

swift - 根据 UITableViewCell 中 UIImageView 中的图像设置 UIView 宽度

ios - 从 UI 选择图像时执行操作