0

我有一个继承自 UIScrollView 的类。drawRect 函数使用 CGLayer 来缓存绘图以实现更快的滚动。不幸的是,每次我尝试滚动视图时代码都会崩溃。

只要在 iOS 模拟器中单击并拖动,它就会崩溃并出现以下错误:

2012-08-04 19:11:09.687 scrolllayertest[3197:c07]-[__NSCFType animationKeys]:无法识别的选择器发送到实例 0x682aa20 2012-08-04 19:11:09.689 scrolllayertest[3197:c07] * 由于未捕获而终止应用程序exception 'NSInvalidArgumentException', reason: '-[__NSCFType animationKeys]: unrecognized selector sent to instance 0x682aa20' * First throw call stack: (0x14b2022 0xeb2cd6 0x14b3cbd 0x1418ed0 0x1418cb2 0x4ca94 0x492a5 0x49459 0x5e157 0x49765 0x1449f1a 0x1415635 0x1415026 0x4966b 0x49765 0x1449f1a 0x1415635 0x1415026 0x4966b 0x43dbd 0x15ca0 0x139cef5 0x1486195 0x13eaff2 0x13e98da 0x13e8d84 0x13e8c9b 0x139b7d8 0x139b88a 0x15626 0x21ed 0x2155 0x1) 终止调用抛出异常

我无法弄清楚问题所在。我将代码粘贴在下面。这也在 iPad 模拟器上的 iOS 5.1 中。我还没有在设备上测试它。

层测试.h

#import <UIKit/UIKit.h>

@interface LayerTest : UIScrollView

@end

层测试.m

#import "LayerTest.h"

@interface LayerTest ()
@property (nonatomic,assign) CGLayerRef layer;
@end

@implementation LayerTest
@synthesize layer;

- (void)setLayer:(CGLayerRef)aLayer {
    if (layer)
        CGLayerRelease(layer);
    layer = aLayer;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)dealloc {
    self.layer = nil;
}

- (void)awakeFromNib {
    CGSize sz = self.bounds.size;
    sz.width *= 2;
    self.contentSize = sz;
    self.layer = nil;
}

- (void)setContentOffset:(CGPoint)contentOffset {
    [super setContentOffset:contentOffset];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    if (self.layer == nil) {
        self.layer = CGLayerCreateWithContext(context, self.contentSize, NULL);
        CGContextRef contextLayer = CGLayerGetContext(self.layer);
        CGRect contentRect = CGRectMake(0, 0, self.contentSize.width, self.contentSize.height);
        CGContextSetFillColorWithColor(contextLayer, [[UIColor blackColor] CGColor]);
        CGContextFillRect(contextLayer, contentRect);
        CGContextSetFillColorWithColor(contextLayer, [[UIColor yellowColor] CGColor]);
        CGContextFillEllipseInRect(contextLayer, contentRect);
    }
    if (self.layer)
        CGContextDrawLayerAtPoint(context, CGPointZero, self.layer);
}

@end

有关如何修复的任何建议?

4

0 回答 0