Ios 如何将边框添加到此分隔层?
我有一张图片,上面我用图层和遮罩划出了一个透明的矩形。 我希望此透明矩形为红色边框。但我可以找到一种方法来实现这一点: 以下是我所做的: 我的ViewController具有DarkedView属性Ios 如何将边框添加到此分隔层?,ios,Ios,我有一张图片,上面我用图层和遮罩划出了一个透明的矩形。 我希望此透明矩形为红色边框。但我可以找到一种方法来实现这一点: 以下是我所做的: 我的ViewController具有DarkedView属性 - (void)loadView { UIView *const rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; rootView.backgroundColor = [UIColor whit
- (void)loadView {
UIView *const rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
rootView.backgroundColor = [UIColor whiteColor];
self.view = rootView;
[self addContentSubviews];
}
- (void)addContentSubviews {
UIImageView *const imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DSC_0823.jpg"]];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.frame = self.view.bounds;
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:imageView];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self addDarkenedSubview];
}
- (void)viewDidLayoutSubviews {
CGRect const bounds = self.view.bounds;
darkenedView.center = CGPointMake(CGRectGetMidX(bounds), 0);
}
- (void)addDarkenedSubview {
darkenedView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 400, 1200)];
darkenedView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
darkenedView.autoresizingMask = 0;
[self.view addSubview:darkenedView];
[self addMaskToDarkenedView];
}
- (void)addMaskToDarkenedView {
CGRect bounds = darkenedView.bounds;
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
CGRect const myRect = CGRectMake(CGRectGetMidX(bounds) - 100,
CGRectGetMidY(bounds) + 100,
200, 200);
UIBezierPath *path = [UIBezierPath bezierPathWithRect:myRect];
[path appendPath:[UIBezierPath bezierPathWithRect:bounds]];
maskLayer.path = path.CGPath;
maskLayer.fillRule = kCAFillRuleEvenOdd;
darkenedView.layer.mask = maskLayer;
}
我试过但没有成功:
maskLayer.strokeColor = [UIColor redColor].CGColor;
maskLayer.lineWidth = 3.0f;
下面的示例代码不是给maskLayer对象添加笔划,而是创建CAShapeLayer并将Sublayer添加到darkenedView视图,希望对您有所帮助
CAShapeLayer *shape = [CAShapeLayer layer];
shape.frame = darkenedView.bounds;
shape.path = path.CGPath;
shape.lineWidth = 3.0f;
shape.strokeColor = [UIColor redColor].CGColor;
shape.fillColor = [UIColor clearColor].CGColor;
[darkenedView.layer addSublayer:shape];
或者为子视图添加一个子层到
darkenedView
,该子视图具有rectCGRectMake(CGRectGetMidX(bounds)-100,CGRectGetMidY(bounds)+100200200)
欢迎并感谢您接受Response@Reveclair。。。!