2014-11-04 91 views
0

我在尝试在自定义UIControl子类中绘制几个CAShapeLayer时出现了一个奇怪的问题。我相信这是显而易见的,但在24小时后,我无法完成工作。CAShapeLayers不绘制自定义子类(UIControl)

我做这样的:

// .h 
@interface IntercomButton : UIControl 

@end 

// .m 
@interface IntercomButton() 
@property (nonatomic) CAShapeLayer *outerCircle, *innerCircle; 
@end 

@implementation IntercomButton 

- (instancetype)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     _innerCircle = [CAShapeLayer layer]; 
     _outerCircle = [CAShapeLayer layer]; 

     _innerCircle.frame = frame; 
     _outerCircle.frame = frame; 

     [self.layer addSublayer:_innerCircle]; 
     [self.layer addSublayer:_outerCircle]; 
    } 
    return self; 
} 

- (void)layoutSubviews { 

    _innerCircle.frame = self.frame; 
    _innerCircle.strokeColor = NULL; 
    _innerCircle.fillColor = [UIColor colorWithRed:0.377 green:0.664 blue:0.894 alpha:1.000].CGColor; 
    _innerCircle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.frame, 12, 12)].CGPath; 
    //_innerCircle.rasterizationScale = 2.0 * [UIScreen mainScreen].scale; 
    //_innerCircle.shouldRasterize = YES; 
    //[self.layer addSublayer:_innerCircle]; 

    _outerCircle.frame = self.frame; 
    _outerCircle.lineWidth = 4.0f; 
    _outerCircle.strokeColor = [UIColor colorWithRed:0.377 green:0.664 blue:0.894 alpha:1.000].CGColor; 
    _outerCircle.fillColor = NULL; 
    _outerCircle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.frame, 6, 6)].CGPath; 
    //_outerCircle.rasterizationScale = 2.0 * [UIScreen mainScreen].scale; 
    //_outerCircle.shouldRasterize = YES; 
    //[self.layer addSublayer:_outerCircle]; 
} 

@end 

另一种观点认为它是被这样创建的视图(它是在一个UITableViewCell,不是,我认为它很重要,但仍...)

IntercomButton *intercomButton = [[IntercomButton alloc] initWithFrame:btn.frame]; 
intercomButton.backgroundColor = [UIColor clearColor]; 
[detailView addSubview:intercomButton]; 

UIControl视图它被添加好,因为如果我改变背景颜色,它会显示。

而且,如果我使用的drawRect绘制,它的工作原理:

- (void)drawRect:(CGRect)rect { 
    [super drawRect:rect]; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.377 green:0.664 blue:0.894 alpha:1.000].CGColor); 
    CGContextFillEllipseInRect (context, CGRectInset(rect, 12, 12)); 
    CGContextFillPath(context); 

    CGContextSetLineWidth(context, 3.0f); 
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.377 green:0.664 blue:0.894 alpha:1.000].CGColor); 
    CGContextStrokeEllipseInRect(context, CGRectInset(rect, 8, 8)); 
    CGContextStrokePath(context); 
} 

的问题是,我需要动画,所以我需要使用CAShapeLayers。 什么我想画应该是这样的(这是它的外观采用的drawRect :)

enter image description here

回答

0

好吧,我真该死,问题是用做

CGRectInset(self.frame, X, X) 

当我应该已经做

CGRectInset(self.bounds, X, X) 

那么,得到位于该视图帧以外THA层。

正如我所说,它结束了一些明显的事情。