我在屏幕上有多个正在移动的形状。
每个形状都可以根据其位置而变化,并使用 CoreGraphics 填充渐变。
其性能非常糟糕 - iPad3 上约为 9fps。
有什么加快速度的技巧吗?有一个预渲染的位图并改变它的形状会有所帮助吗?
我正在重新计算形状(三角形)并重新绘制每个动作:
- (void) drawGradientShapeInRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat start_x = self.frame.size.width / 2.0;
CGFloat start_y = self.frame.size.height;
CGFloat end_x = radius_ * cos(angleInRad_) + start_x;
CGFloat end_y = MAX(radius_ * sin(angleInRad_) + start_y, self.bounds.size.height - constrainedHeight_);
// Create Lines
CGPoint startPt = CGPointMake(start_x, start_y);
CGContextMoveToPoint(context, self.center.x, self.center.y);
CGPoint addLines[] =
{
startPt,
CGPointMake(end_x, end_y),
CGPointMake(end_x, start_y),
startPt
};
CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));
// Setup Gradient
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
NSArray *colors = nil;
if(start_x > end_x)
{
colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0].CGColor,
(id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f].CGColor,
nil];
}
else
{
colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f].CGColor,
(id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0].CGColor,
nil];
}
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
CGColorSpaceRelease(space);
CGColorSpaceRelease(rgb);
CGPoint startGradientPt = CGPointMake((start_x < end_x) ? start_x : end_x, 0);
CGPoint endGradientPt = CGPointMake((start_x > end_x) ? start_x : end_x, 0);
CGContextSaveGState(context);
CGContextClip(context);
CGContextDrawLinearGradient(context,
gradient,
startGradientPt,
endGradientPt,
0);
CGGradientRelease(gradient);
CGContextRestoreGState(context);
}