iOS:为什么UIView的drawRect比CALayer的drawInContext

时间:2015-12-01 11:54:05

标签: ios uiview calayer drawrect

我目前正在学习使用UIView和CALayer绘图。我很快做了一个绘图应用程序来试验这两个类。我注意到CALayer的表现比UIView差。这是代码:

myView.m

@interface myView()

@property (nonatomic, strong) UIImageView *background;

#ifdef USE_LAYER
@property (nonatomic, strong) drawingLayer *drawCanvas;
#else
@property (nonatomic, strong) drawingView  *drawCanvas;
#endif
@end

@implementation myView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.background = [[UIImageView alloc] initWithFrame:frame];
        [self addSubview:self.background];
#ifdef USE_LAYER
        self.drawCanvas = [[drawingLayer alloc] init];
        self.drawCanvas.frame = frame;
        [self.layer addSublayer:self.drawCanvas];
#else
        self.drawCanvas = [[drawingView alloc] initWithFrame:frame];
        self.drawCanvas.backgroundColor = [UIColor clearColor];
        [self addSubview:self.drawCanvas];
#endif
    }
    return self;
}

- (CGPoint)getPoint:(NSSet<UITouch *> *)touches
{
    NSArray *touchesArray = [touches allObjects];
    UITouch *touch = (UITouch *)[touchesArray objectAtIndex:0];
    return [touch locationInView:self];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [self getPoint:touches];
    self.drawCanvas.points = [[NSMutableArray alloc] init];
    self.drawCanvas.startPoint = point;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [self getPoint:touches];
    [self.drawCanvas.points addObject:[NSValue valueWithCGPoint:point]];
    self.background.image = [self imageFromLayer:self.layer];
    self.drawCanvas.points = nil;
    [self.drawCanvas setNeedsDisplay];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [self getPoint:touches];
    [self.drawCanvas.points addObject:[NSValue valueWithCGPoint:point]];
    [self.drawCanvas setNeedsDisplay];
}

- (UIImage *)imageFromLayer:(CALayer *)layer
{
    UIGraphicsBeginImageContext([layer frame].size);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return outputImage;
}

@end

drawingView.h

@interface drawingView : UIView

@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, strong) NSMutableArray *points;

@end

drawingView.m

@implementation drawingView

- (void)drawRect:(CGRect)rect {
     CGContextRef context = UIGraphicsGetCurrentContext();
     CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

     CGContextSetLineWidth(context, 2.0f);
     CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y);

     for (NSValue *point in self.points)
     {
         CGPoint location = [point CGPointValue];
         CGContextAddLineToPoint(context, location.x, location.y);
     }
     CGContextStrokePath(context);
}

@end

我也有&#34; drawingLayer&#34;这与&#34; drawingView&#34;基本相同,除了它是CALayer的子类,并在&#34; drawInContext&#34;中执行相同的绘制。

我发现drawRect具有更好的性能,绘图几乎与触摸位置同步,具有非常小的延迟。 为什么会这样?我期望CALayer绘图至少相同,如果不是更好的话。事实上,有人今天告诉我,CALayer中的绘图是硬件加速的,如果性能是一个问题,这是首选方式。当然,在我的小实验中并非如此。为什么呢?

1 个答案:

答案 0 :(得分:0)

这可能是由CALayer的默认动画引起的。

尝试覆盖action(forKey event: String) -> CAAction?并在继承CALayer类时始终返回nil