大卫是对的,您实际上并没有更改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