它之外的iOS笔划层';界
我正在尝试在它之外的iOS笔划层';界,ios,uiview,core-graphics,calayer,quartz-core,Ios,Uiview,Core Graphics,Calayer,Quartz Core,我正在尝试在UIView上构建一个category方法,以允许在UIView上划一条线,类似于Photoshop函数:划一条线。虽然CALayerproperties borderWidth和borderColor可以完美地在内部进行笔划,但我找不到在外部进行笔划的好方法 这是我首先想到的解决方案: 使用子层,我添加了另一个CALayer,其帧大于超层的边界。然后将该层插入到UIView的子层中 这适用于所有的UIView,这些视图的图层属性masksToBounds或视图属性clipstobu
UIView
上构建一个category方法,以允许在UIView
上划一条线,类似于Photoshop函数:划一条线。虽然CALayer
properties borderWidth和borderColor可以完美地在内部进行笔划,但我找不到在外部进行笔划的好方法
这是我首先想到的解决方案:
使用子层,我添加了另一个CALayer
,其帧大于超层的边界。然后将该层插入到UIView
的子层中
这适用于所有的UIView
,这些视图的图层属性masksToBounds
或视图属性clipstobunds
设置为NO
。但这会禁用使用cornerRadius属性的功能,我希望这样做
因此,我的第二次尝试是使用UIView
,因为很明显,我必须离开我想要划过的视图(我假设相同的逻辑可以应用于层和超层关系)。
我提出了这个解决方案:
- (void)strokeViewWithColor:(UIColor *)color borderWidth:(CGFloat)borderWidth
{
UIView* superview = self.superview;
//
// Fix frame
//
CGRect frame = self.frame;
frame.origin.x -= borderWidth;
frame.origin.y -= borderWidth;
frame.size.width += (2 * borderWidth);
frame.size.height += (2 * borderWidth);
UIView* strokeView = [[UIView alloc] initWithFrame:frame];
strokeView.layer.borderWidth = borderWidth;
strokeView.layer.cornerRadius = self.layer.cornerRadius + borderWidth;
strokeView.layer.borderColor = [color CGColor];
[superview insertSubview:strokeView aboveSubview:self];
}
这是可行的,但问题在于自动布局。由于strokeView
没有自动布局约束,因此添加约束时可能无法正常工作
这就是为什么我在寻找任何其他解决方案
解决这个问题的最佳方法是什么?我应该写一些代码来处理代码第二部分的自动布局问题吗?或者我应该从不同的角度来处理这个问题?如果是,怎么做?
我主要是在寻找如何解决这个问题的建议
谢谢你的帮助
- (void)strokeViewWithColor:(UIColor *)color borderWidth:(CGFloat)borderWidth
{
UIView* superview = self.superview;
//
// Fix frame
//
CGRect frame = self.frame;
frame.origin.x -= borderWidth;
frame.origin.y -= borderWidth;
frame.size.width += (2 * borderWidth);
frame.size.height += (2 * borderWidth);
UIView* strokeView = [[UIView alloc] initWithFrame:frame];
strokeView.layer.borderWidth = borderWidth;
strokeView.layer.cornerRadius = self.layer.cornerRadius + borderWidth;
strokeView.layer.borderColor = [color CGColor];
[superview insertSubview:strokeView aboveSubview:self];
}