在我的應用程序中,我將一個自定義視圖設置爲navigationBar.titleView,並將tableView添加到控制器。在自定義視圖中有一個名爲GSSearchIcon的UIView子類和一個UILabel。爲什麼當CAShapeLayer顏色改變時有一點延遲?
我想要做的是,當滾動的tableView navigationBar.titleView(GSSearchIcon和UILabel的文字顏色的顏色)的顏色在基於tableView.contentOffset
同時改變但是,當tableView滾動時,UILabel的textColor會立即改變,但GSSearchIcon的顏色改變有點延遲,可能會延長几百毫秒。
GSSearchIcon代碼如下
#import "GSSearchIcon.h"
static const CGFloat kCircleRadius = 6.3;
static const CGFloat kIconWidth = 16;
@interface GSSearchIcon()
@property (nonatomic, strong) CAShapeLayer *circleLayer;
@property (nonatomic, strong) CAShapeLayer *lineLayer;
@end
@implementation GSSearchIcon
- (instancetype)initIconWithFrame:(CGRect)frame {
self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, kIconWidth, kIconWidth)];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
self.backgroundColor = [UIColor clearColor];
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(kCircleRadius, kCircleRadius) radius:kCircleRadius startAngle:0 endAngle:2 * M_PI clockwise:YES];
_circleLayer = [[CAShapeLayer alloc] init];
[_circleLayer setPath:circlePath.CGPath];
[_circleLayer setStrokeColor:[UIColor gs_colorWithSameRGB:255 alpha:1].CGColor];
[_circleLayer setFillColor:[UIColor clearColor].CGColor];
[_circleLayer setLineWidth:1];
[_circleLayer setStrokeStart:0];
[_circleLayer setStrokeEnd:2 * M_PI];
[self.layer addSublayer:_circleLayer];
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(16 - (16 - 2 * kCircleRadius)* sqrt(2), 16 - (16 - 2 * kCircleRadius)* sqrt(2))];
[linePath addLineToPoint:CGPointMake(16, 16)];
_lineLayer = [[CAShapeLayer alloc] init];
[_lineLayer setPath:linePath.CGPath];
[_lineLayer setStrokeColor:[UIColor gs_colorWithSameRGB:255 alpha:1].CGColor];
[_lineLayer setFillColor:[UIColor clearColor].CGColor];
[_lineLayer setLineWidth:1];
[self.layer addSublayer:_lineLayer];
}
- (void)setColor:(UIColor *)color {
[self.circleLayer setStrokeColor:color.CGColor];
[self.lineLayer setStrokeColor:color.CGColor];
}
@end
難道這引起CAShapeLayer性能問題?以及如何解決它?提前致謝。
它的工作原理! 「UIImageRenderingModeAlwaysTemplate」是神奇的。非常感謝〜 – highsun16
酷!很高興聽到它的工作。使用模板渲染模式確實是魔術所在的地方.. – Artal