- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
如何使用 CAShapeLayer 绘制一条同时具有边框颜色、边框宽度和填充颜色的线条?
这是我试过的方法,但它永远是蓝色的...
self.lineShape.strokeColor = [UIColor blueColor].CGColor;
self.lineShape.fillColor = [UIColor greenColor].CGColor;
self.lineShape.lineWidth = 100;
self.lineShape.lineCap = kCALineCapRound;
self.lineShape.lineJoin = kCALineJoinRound;
UIBezierPath* path = [UIBezierPath bezierPath];
[path moveToPoint:self.lineStart];
[path addLineToPoint:self.lineEnd];
self.lineShape.path = path.CGPath;
最佳答案
Swift 3+ 扩展方法(结合下面的 AechoLiu 回答):
// Usage:
self.btnGroup.roundCorner([.topRight, .bottomRight], radius: 4.0, borderColor: UIColor.red, borderWidth: 1.0)
// Apply round corner and border. An extension method of UIView.
public func roundCorner(_ corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
let path = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
let borderPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let borderLayer = CAShapeLayer()
borderLayer.path = borderPath.cgPath
borderLayer.lineWidth = borderWidth
borderLayer.strokeColor = borderColor.cgColor
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.frame = self.bounds
self.layer.addSublayer(borderLayer)
}
objective-C :
self.lineShapeBorder = [[CAShapeLayer alloc] init];
self.lineShapeBorder.zPosition = 0.0f;
self.lineShapeBorder.strokeColor = [UIColor blueColor].CGColor;
self.lineShapeBorder.lineWidth = 25;
self.lineShapeBorder.lineCap = kCALineCapRound;
self.lineShapeBorder.lineJoin = kCALineJoinRound;
self.lineShapeFill = [[CAShapeLayer alloc] init];
[self.lineShapeBorder addSublayer:self.lineShapeFill];
self.lineShapeFill.zPosition = 0.0f;
self.lineShapeFill.strokeColor = [UIColor greenColor].CGColor;
self.lineShapeFill.lineWidth = 20.0f;
self.lineShapeFill.lineCap = kCALineCapRound;
self.lineShapeFill.lineJoin = kCALineJoinRound;
// ...
UIBezierPath* path = [UIBezierPath bezierPath];
[path moveToPoint:self.lineStart];
[path addLineToPoint:self.lineEnd];
// call addLineToPoint over and over to create the outline for the shape
self.lineShapeBorder.path = self.lineShapeFill.path = path.CGPath;
同时调用 @FarrasDoko 评论尝试将 borderWidth
更改为 lineWidth
并将 borderColor
更改为 strokeColor
。
关于iphone - CAShapeLayer 具有边框和填充颜色和舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12756928/
在 Swift 中,我有两个半透明圆圈,它们都是 CAShapeLayer。由于它们是半透明的,因此它们之间的任何重叠都变得可见,如下所示: 相反,我希望它们在视觉上“合并”在一起。我尝试过的解决方案
我注意到大多数人在初始化 CAShapeLayer 时使用: CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 而不是使用初始化器: CAShapeLa
我有一个 CAShapeLayer,它必须在用户手指的引导下完成在屏幕上移动的简单任务。 问题是 Action 太慢了。图层确实移动了,但有延迟,感觉很慢。 我有另一个测试应用程序,其中 UIImag
有人有将渐变应用于 CAShapeLayer 的经验吗? CAShapeLayer 是一个很棒的图层类,但它似乎只支持纯色填充着色,而我希望它具有渐变填充(实际上是一个可设置动画的渐变)。 与 CAS
我正在使用核心动画层制作“行进的 Ant ”选择框架,如本文所示:MARCHING ANTS WITH CORE ANIMATION 。但我唯一不喜欢的是 CAShapeLayer 中的线宽。我需要宽
我目前正在使用 CAShapeLayer对于我的要求之一。我成功地实现了 fillcolor、strokecolor 等...但是我想更改该 CAShapeLayer 外部的颜色。我尝试用 backg
使用该代码,我从第一次触摸(触摸开始)和最后一次触摸(触摸结束)创建一行。但为了使这个形状动态(调整大小和移动),我需要在触摸它时选择它。我怎样才能做到这一点? shape.opacity =
我一直在尝试向我的 View 和按钮添加一个图层,但不知何故该图层根本没有出现。我错过了什么? class ViewController: UIViewController { var but
我正在尝试在嵌套在 UIView 层中的 CAShapeLayer 内渲染静态图像。形状的所有装饰(即边框、颜色、路径等)都会在 View 中正确渲染。 但是,为CALayer.contents属性提
我尝试在 UIImageView 中居中 CAShapeLayer 但没有成功,我找到了解决方案,将其(CAShapeLayer)添加到 UIVIew,然后将 UIVIew 添加到 ImageView
我正在为 CAShapeLayer 的结束笔画设置动画,但位置不正确,我无法弄清楚我做错了什么。框架是正确的,但是当我尝试调整位置时,我无法让它“填充” View 框架的宽度。我试图调整 _maskL
我正在尝试相对于特定 anchor 旋转 CAShapeLayer。但是当我申请firstLayer.transform = CATransform3DMakeRotation(CGFloat(M_2
我有一个简单的循环进度 View ,当下载文件时该 View 会被填充。大多数时候它会按预期工作,但有时它会“重置” - 它会填充到某个部分,通常填充到 100% 之前的最后一部分,然后突然变为 0%
我的 UIView 子类中有以下代码的三个变体。 局部变量 - (void)setupLayer { CAShapeLayer *faucet = [CAShapeLayer layer];
我正在使用 UIPickerView,它从具有十六进制颜色代码列表的数组中读取,然后用于用所有不同的颜色填充选择器 View 的每一行。我正在使用一种将十六进制代码转换为 RGB 代码的方法,以及一个
我在屏幕上总共画了 21 条线作为测量工具。这些线条在 UIViewController.view 上绘制为图层。我正在尝试按如下方式删除这些行。 NSLog 声明确认我得到了我正在寻找的所有 21
我正在开发 iPhone 应用程序,我使用下一个代码来圆我层的两个角: CAShapeLayer *backgroundMaskLayer = [CAShapeLayer layer]; UIBezi
我正在尝试围绕其中心点旋转 CAShapeLayer。我对边界、框架、 anchor 和中心点感到非常困惑。 我在 UIView 中创建了一个圆形 CAShape,并希望围绕其中心旋转它。我只包含了部
我正在尝试创建一个动画,其中两条线在用户拖动 UIView 时淡出并在用户释放拖动时淡入。 因此,我有两个函数 undrawLines(在平移手势开始时调用)和 redrawLines(在平移手势结束
我想知道为什么当我尝试使用基本动画为 CAShapeLayer 的 path 属性设置动画时它可以工作,但当我尝试使用事务来执行此操作时却不起作用。 我已经使用事务成功地为其他 animatable
我是一名优秀的程序员,十分优秀!