0

我有一个带有 imageView 属性的 uitableviewcell 子类。我从网上获取图像,当我得到它时,我想将它淡入淡出到图像视图中。

我有这个 CAAnimation 代码,我最初在视图控制器中使用它来应用这个效果。我的代码如下所示:

    CATransition *transition = [CATransition animation];
    transition.duration = 0.3f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    [self.photoView.layer addAnimation:transition forKey:someKey];

现在,我想将该代码移到我的 UITableViewCell 中。我已经尝试在不同的位置应用该动画,但它似乎没有影响。我把它放在 awakeFromNib(单元格来自一个 nib 文件)以及 willMoveToSuperview 中。有什么想法吗?

4

1 回答 1

0

大卫是对的,您实际上并没有更改imageView代码中的内容。

我刚刚创建了一个的这个子类UITableViewCell并将其添加到一个基本的 iOS 单视图应用程序中:

#import "AnimatedCell.h"

@implementation AnimatedCell {
    BOOL imageWasGrabbed;
}

- (id)initWithStyle:(UITableViewCellStyle)style 
    reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.frame = CGRectMake(0,0,320,44);
        self.imageView.image = [UIImage imageNamed:@"localImage"];
        self.textLabel.text = @"Locally loaded image";
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return self;
}

-(void)grabImage {
    NSString *path = @"http://www.c4ios.com/images/temp/webImage@2x.png";
    NSURL *webImageUrl = [NSURL URLWithString:path];
    NSData *webImageData = [NSData dataWithContentsOfURL:webImageUrl];
    UIImage *image = [UIImage imageWithData:webImageData];

    CATransition *transition = [CATransition animation];
    transition.duration = 1.0f;
    transition.delegate = self;
    NSString *n = kCAMediaTimingFunctionEaseInEaseOut;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:n];
    transition.type = kCATransitionFade;

    [self.imageView.layer addAnimation:transition forKey:@"animateContents"];

    self.imageView.image = image;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if(!imageWasGrabbed) {
        [self grabImage];
    }
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    self.textLabel.text = @"Web loaded image";
    imageWasGrabbed = YES;
}

@end

将过渡添加到单元格的 imageView 层后,您需要调用以下命令:

self.imageView.image = image;

作为参考,我的项目的视图控制器如下所示:

#import "ViewController.h"
#import "AnimatedCell.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    AnimatedCell *cell = [[AnimatedCell alloc]
                          initWithStyle:UITableViewCellStyleDefault
                          reuseIdentifier:@"AnimatedCell"];
    cell.center = self.view.center;
    [self.view addSubview:cell];
}

@end
于 2013-09-20T21:26:23.860 回答