6
我试了几个小时,用CAShapeLayer在我的UIView周围得到了一个虚线的边框,但是我没有看到它。
ScaleOverlay.h在UIView子类中使用的CAShapeLayer不起作用
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ScaleOverlay : UIView <UIGestureRecognizerDelegate> {
CAShapeLayer *shapeLayer_;
}
@end
ScaleOverlay.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor redColor];
self.alpha = 0;
//Round corners
[[self layer] setCornerRadius:8.f];
[[self layer] setMasksToBounds:YES];
//Border
shapeLayer_ = [[CAShapeLayer layer] retain];
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, frame);
shapeLayer_.path = path;
CGPathRelease(path);
shapeLayer_.backgroundColor = [[UIColor clearColor] CGColor];
shapeLayer_.frame = frame;
shapeLayer_.position = self.center;
[shapeLayer_ setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];
shapeLayer_.fillColor = [[UIColor blueColor] CGColor];
shapeLayer_.strokeColor = [[UIColor blackColor] CGColor];
shapeLayer_.lineWidth = 4.;
shapeLayer_.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:8], [NSNumber numberWithInt:8], nil];
shapeLayer_.lineCap = kCALineCapRound;
}
return self;
}
我在我的上海华绘制的红色矩形,但没有边界。从一个源代码复制这个例子,希望它可以工作,但事实并非如此。
不能相信我忘了'[self.layer addSublayer:shapeLayer _];'并且搜索到天数 – Seega 2011-03-28 07:22:55
由于某种原因,您不能使用CAShapeLayer作为背景层。 – openfrog 2013-05-01 20:34:38
@openfrog我看到了完全相同的行为。很奇怪... – LucasTizma 2017-03-18 22:01:21