CALayer自定义绘图故障

时间:2014-10-09 10:53:05

标签: ios xcode core-graphics calayer

我正在研究发光效果 CALayer 。我创建了一个UIView类别。我的自定义绘图给了我这个奇怪的动画:

enter image description here

请检查我的full code.

更新
我添加了一些我觉得至关重要的代码。

- (void)setGlowHidden:(BOOL)glowHidden {
  self.layer.masksToBounds = glowHidden;
  self.clipsToBounds = glowHidden;
  if (self.glowLayer == nil) {
    self.glowLayer = [GlowLayer layer];
    self.glowLayer.frame = self.layer.bounds;
    self.glowLayer.parentView = self;
    self.glowLayer.masksToBounds = NO;
    self.glowLayer.contentsGravity = kCAGravityCenter;
    [self.layer addSublayer:self.glowLayer];
  }
  //...
  [self.glowLayer setNeedsDisplay];
  [self.glowLayer performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0.0];
}

//...

- (void)drawInContext:(CGContextRef)ctx {
  CGFloat delta = MAX(self.glowSize, self.innerGlowSize);
  CGRect drawingRect = CGRectMake(-delta, -delta, CGRectGetWidth(self.parentView.frame) + 2 * delta,
                                  CGRectGetHeight(self.parentView.frame) + 2 * delta);
  CGPoint offsetPoint = CGPointMake(-drawingRect.origin.x, -drawingRect.origin.y);
  self.frame = drawingRect;
  [self setNeedsLayout];
  [self layoutIfNeeded];

  UIGraphicsBeginImageContextWithOptions(drawingRect.size, NO, 0.0f);

//  [self.parentView drawViewHierarchyInRect:rect afterScreenUpdates:YES];
  [self.parentView.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *textImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  UIGraphicsPushContext(ctx);
  CGContextSaveGState(ctx);
  if (_glowSize > 0) {
    CGContextSetShadow(ctx, CGSizeZero, _glowSize);
    CGContextSetShadowWithColor(ctx, CGSizeZero, _glowSize, _glowColor.CGColor);
  }
  [textImage drawAtPoint:offsetPoint];

  CGContextRestoreGState(ctx);
  //...
}

0 个答案:

没有答案