2016-01-18 34 views
0

在我的應用程序中,我將一個自定義視圖設置爲navigationBar.titleView,並將tableView添加到控制器。在自定義視圖中有一個名爲GSSearchIcon的UIView子類和一個UILabel。爲什麼當CAShapeLayer顏色改變時有一點延遲?

它看起來像這樣 enter image description here

我想要做的是,當滾動的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性能問題?以及如何解決它?提前致謝。

回答

1

我不知道如何調用setColor方法的代碼看起來像,那裏可能會發生一些事情。

然而,如果一切都看起來不錯,它實際上是與實際發生的顏色變化時的性能問題,我可以提出一個更快的替代方案:

  1. 而不是你GSSearchIcon的,使用普通的UIImageView
  2. 您可以使用PNG文件作爲圖標,也可以使用您已有的代碼自行繪製它。
  3. 使用「始終模板」呈現模式創建圖像,以便通過更改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]; 

我很想知道這種方法是否可以修復延遲..

+0

它的工作原理! 「UIImageRenderingModeAlwaysTemplate」是神奇的。非常感謝〜 – highsun16

+0

酷!很高興聽到它的工作。使用模板渲染模式確實是魔術所在的地方.. – Artal

相關問題