【问题标题】:Adding CAAnimation to sublayer in UITableViewCell将 CAAnimation 添加到 UITableViewCell 中的子层
【发布时间】:2013-09-19 22:10:56
【问题描述】:

我有一个带有 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 中。有什么想法吗?

【问题讨论】:

  • 您的转换似乎没有改变照片视图上的任何内容

标签: ios core-animation calayer


【解决方案1】:

David 是对的,您实际上并没有更改代码中 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 2011-12-01
    • 2012-03-30
    • 1970-01-01
    相关资源
    最近更新 更多