【发布时间】:2014-11-04 09:36:08
【问题描述】:
尝试在自定义 UIControl 子类中绘制几个 CAShapeLayer 时遇到一个奇怪的问题。我确定这很明显,但是 24 小时后,我无法让它工作。
我是这样做的:
// .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的样子:)
【问题讨论】:
标签: ios objective-c uitableview calayer cashapelayer