Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
它之外的iOS笔划层';界_Ios_Uiview_Core Graphics_Calayer_Quartz Core - Fatal编程技术网

它之外的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];
}