在我的应用程序中,我将自定义视图设置为navigationBar.titleView并将tableView添加到控制器。在自定义视图中,有一个名为GSSearchIcon的UIView子类和一个UILabel。
我想要做的是当tableView滚动时,导航颜色(GSBSI的颜色和UILabel的textColor)基于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性能问题引起的吗?以及如何解决它?提前谢谢。
答案 0 :(得分:1)
我不知道调用setColor
方法的代码是怎么样的,可能会有一些事情发生。
但是,如果一切看起来都很好并且实际上是一个性能问题,它实际上需要更改颜色的时间,我可以建议更快的替代方案:
GSSearchIcon
。UIImageView
tintColor
来更改图标颜色。请参阅以下基于您的示例代码:
UIImageView
属性:
@property (nonatomic, strong) UIImageView *searchIconImageView;
绘制图标并创建UIImageView
:
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(kCircleRadius, kCircleRadius) radius:kCircleRadius * 0.92 startAngle:0 endAngle:2 * M_PI clockwise:YES];
[circlePath stroke];
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(16 - (16 - 2 * kCircleRadius)* sqrt(2), 16 - (16 - 2 * kCircleRadius)* sqrt(2))];
[linePath addLineToPoint:CGPointMake(16, 16)];
[linePath stroke];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.searchIconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kIconWidth, kIconWidth)];
self.searchIconImageView.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.searchIconImageView.tintColor = [UIColor whiteColor];
[self.view addSubview:self.searchIconImageView];
更改颜色:
self.searchIconImageView.tintColor = [UIColor redColor];
我很想知道这种方法是否能解决这个问题。