我正在尝试使用 CAlayers 制作逐帧动画。我正在使用本教程http://mysterycoconut.com/blog/2011/01/cag1/执行此操作,但一切都适用于禁用 ARC,当我尝试使用 ARC 重写代码时,它可以在模拟器上完美运行,但在设备上我有一个坏的存取存储器。
层类接口
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
@interface MCSpriteLayer : CALayer {
unsigned int sampleIndex;
}
// SampleIndex needs to be > 0
@property (readwrite, nonatomic) unsigned int sampleIndex;
// For use with sample rects set by the delegate
+ (id)layerWithImage:(CGImageRef)img;
- (id)initWithImage:(CGImageRef)img;
// If all samples are the same size
+ (id)layerWithImage:(CGImageRef)img sampleSize:(CGSize)size;
- (id)initWithImage:(CGImageRef)img sampleSize:(CGSize)size;
// Use this method instead of sprite.sampleIndex to obtain the index currently displayed on screen
- (unsigned int)currentSampleIndex;
@end
层类实现
@implementation MCSpriteLayer
@synthesize sampleIndex;
- (id)initWithImage:(CGImageRef)img;
{
self = [super init];
if (self != nil)
{
self.contents = (__bridge id)img;
sampleIndex = 1;
}
return self;
}
+ (id)layerWithImage:(CGImageRef)img;
{
MCSpriteLayer *layer = [(MCSpriteLayer*)[self alloc] initWithImage:img];
return layer ;
}
- (id)initWithImage:(CGImageRef)img sampleSize:(CGSize)size;
{
self = [self initWithImage:img]; // IN THIS LINE IS BAD ACCESS
if (self != nil)
{
CGSize sampleSizeNormalized = CGSizeMake(size.width/CGImageGetWidth(img), size.height/CGImageGetHeight(img));
self.bounds = CGRectMake( 0, 0, size.width, size.height );
self.contentsRect = CGRectMake( 0, 0, sampleSizeNormalized.width, sampleSizeNormalized.height );
}
return self;
}
+ (id)layerWithImage:(CGImageRef)img sampleSize:(CGSize)size;
{
MCSpriteLayer *layer = [[self alloc] initWithImage:img sampleSize:size];
return layer;
}
+ (BOOL)needsDisplayForKey:(NSString *)key;
{
return [key isEqualToString:@"sampleIndex"];
}
// contentsRect or bounds changes are not animated
+ (id < CAAction >)defaultActionForKey:(NSString *)aKey;
{
if ([aKey isEqualToString:@"contentsRect"] || [aKey isEqualToString:@"bounds"])
return (id < CAAction >)[NSNull null];
return [super defaultActionForKey:aKey];
}
- (unsigned int)currentSampleIndex;
{
return ((MCSpriteLayer*)[self presentationLayer]).sampleIndex;
}
// Implement displayLayer: on the delegate to override how sample rectangles are calculated; remember to use currentSampleIndex, ignore sampleIndex == 0, and set the layer's bounds
- (void)display;
{
if ([self.delegate respondsToSelector:@selector(displayLayer:)])
{
[self.delegate displayLayer:self];
return;
}
unsigned int currentSampleIndex = [self currentSampleIndex];
if (!currentSampleIndex)
return;
CGSize sampleSize = self.contentsRect.size;
self.contentsRect = CGRectMake(
((currentSampleIndex - 1) % (int)(1/sampleSize.width)) * sampleSize.width,
((currentSampleIndex - 1) / (int)(1/sampleSize.width)) * sampleSize.height,
sampleSize.width, sampleSize.height
);
}
@end
我在 viewDidAppear 上创建图层并通过单击按钮开始动画,但是在初始化之后我得到了一个错误的访问错误
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSString *path = [[NSBundle mainBundle] pathForResource:@"mama_default.png" ofType:nil];
CGImageRef richterImg = [UIImage imageWithContentsOfFile:path].CGImage;
CGSize fixedSize = animacja.frame.size;
NSLog(@"wid: %f, heigh: %f", animacja.frame.size.width, animacja.frame.size.height);
NSLog(@"%f", animacja.frame.size.width);
richter = [MCSpriteLayer layerWithImage:richterImg sampleSize:fixedSize];
animacja.hidden = 1;
richter.position = animacja.center;
[self.view.layer addSublayer:richter];
}
-(IBAction)animacja:(id)sender
{
if ([richter animationForKey:@"sampleIndex"])
{NSLog(@"jest");
}
if (! [richter animationForKey:@"sampleIndex"])
{
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"sampleIndex"];
anim.fromValue = [NSNumber numberWithInt:0];
anim.toValue = [NSNumber numberWithInt:22];
anim.duration = 4;
anim.repeatCount = 1;
[richter addAnimation:anim forKey:@"sampleIndex"];
}
}
你知道如何解决它吗?非常感谢。