标签: cashapelayer

设置 CAShapeLayer lineWidth 小于 1

在上面的屏幕截图中,有两行:

  1. 实线只是UIView高度为 1px 的线
  2. 使用此代码创建虚线

- (void)viewDidAppear:(BOOL)animated {
    CAShapeLayer *line = [CAShapeLayer layer];
    UIBezierPath *linePath=[UIBezierPath bezierPath];

    [linePath moveToPoint:CGPointMake(0, 107)];
    [linePath addLineToPoint:CGPointMake(self.view.frame.size.width, 107)];

    line.lineWidth = 0.5;
    line.path=linePath.CGPath;
    line.fillColor = [[UIColor blackColor] CGColor];
    line.strokeColor = [[UIColor blackColor] CGColor];

    [line setLineJoin:kCALineJoinRound];
    [line setLineDashPattern: [NSArray arrayWithObjects:[NSNumber numberWithInt:10], [NSNumber numberWithInt:5],nil]];

    [[self.view layer] addSublayer:line];
}
Run Code Online (Sandbox Code Playgroud)

为什么UIView1 像素 ( 1.0) 的高度小于虚线的0.5高度?

我希望虚线和实线一样细。

objective-c cashapelayer ios uibezierpath

1
推荐指数
1
解决办法
1945
查看次数

如何在 Swift 4 中使用 CAShapeLayer 和 BezierPath 绘制曲线?

我想知道如何在 Swift 4 中使用 CAShapeLayer 和 BezierPath 给定两个点(A 点和 B 点)渲染一条如下图所示的曲线?

func drawCurvedLine(start: CGPoint, end: CGPoint) {
   //insert code here
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

core-graphics cashapelayer ios uibezierpath swift4

1
推荐指数
1
解决办法
2986
查看次数

仅在 UIBezierPath() 的角落有边框,lineDashPattern 中的空间错误

我尝试使用 lineDashPattern 创建一个仅在角落有边框(qrCode 样式)的视图,但线之间的空间似乎与我定义的模式不匹配。这是我的游乐场:

override func loadView() {
    let view = UIView()
    view.backgroundColor = .white
    let myView = UIView(frame: CGRect(x: 50, y: 50, width: 200, height: 200))
    myView.backgroundColor = .black
    let borderLayer = CAShapeLayer()
    let d = (myView.bounds.width / 2) as NSNumber
    borderLayer.strokeColor = UIColor.blue.cgColor
    borderLayer.lineDashPattern = [d]
    borderLayer.frame = myView.bounds
    borderLayer.fillColor = nil
    borderLayer.lineWidth = 10
    borderLayer.strokeStart = 0.1
    //borderLayer.lineDashPhase = -80
    borderLayer.path = UIBezierPath(roundedRect: myView.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 30, height: 30)).cgPath
    borderLayer.lineCap = CAShapeLayerLineCap.round
    myView.layer.addSublayer(borderLayer)
    view.addSubview(myView)
    self.view …
Run Code Online (Sandbox Code Playgroud)

uiview cashapelayer ios swift

1
推荐指数
1
解决办法
599
查看次数

图层上的脉冲动画

我正在为一个应用程序制作一些自定义类型的加载程序,我只需在我的视图中制作 4 个圆形层,并且所有设置和显示都很好。但是当我对所有图层应用脉冲动画缩放时,它们会扰乱我的所有设计和图层缩放,但它改变了它的中心点。我希望所有圆形图层首先缩放图层并缩小其尺寸并使其变大,然后再次进行同一性,它应该称为无穷大。但不应该改变它的中心点。

这是我分享我的代码

class ViewController: BaseViewController{
    
    @IBOutlet weak var layerView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        drawCircles()
    }

    func drawCircles(){
        let width = layerView.bounds.width
        let halfOfView = width / 2
        
        let firstCirleLayer = UIBezierPath(arcCenter: CGPoint(x: halfOfView / 2, y: halfOfView), radius: halfOfView /  3.5, startAngle: 0, endAngle: 2 * .pi, clockwise: true)
        circleLayers(path: firstCirleLayer.cgPath, color: UIColor.purple)
        
        let secondCirleLayer = UIBezierPath(arcCenter: CGPoint(x: halfOfView, y: halfOfView / 2), radius: halfOfView /  3.5, startAngle: 0, endAngle: 2 * .pi, clockwise: true)
        circleLayers(path: …
Run Code Online (Sandbox Code Playgroud)

core-animation cabasicanimation cashapelayer swift ios-animations

1
推荐指数
1
解决办法
915
查看次数

如何在使用 CAShapeLayer - Swift 绘制的路径上标记点?

CAShapeLayerUIView. 它是一个半圆,上半部分,从左到右绘制。

以下是使用的代码,写在UIView子类中:

func drawSemicircle() {

        // drawing an upper half of a circle -> 180 degree to 0 degree, clockwise
        let startAngle = CGFloat(M_PI)
        let endAngle = CGFloat(0.0)
        let centerPoint = CGPointMake(CGRectGetWidth(frame)/2 , CGRectGetHeight(frame))

        // path set here
        let semirCircleLayer = CAShapeLayer()
        let semirCirclePath = UIBezierPath(arcCenter:centerPoint, radius: CGRectGetWidth(frame)/2 - 20.0 , startAngle:startAngle, endAngle:endAngle, clockwise: true)
        semirCircleLayer.path = semirCirclePath.CGPath

        // layer customisation
        semirCircleLayer.fillColor = UIColor.clearColor().CGColor
        semirCircleLayer.strokeColor = UIColor(red: 237.0/255.0, green: 236.0/255.0, blue: 236.0/255.0, alpha: …
Run Code Online (Sandbox Code Playgroud)

core-graphics uiview cashapelayer ios swift

0
推荐指数
1
解决办法
1728
查看次数

CAShapeLayer 的位置不正确

我有一个UIViewviewProgress. 它是图像中的白框。我想要一个圆形的进度条,也就是图片中的绿色圆圈。进度条应该留在白框内,但正如您所看到的那样。

我如何让它留在里面viewProgress?在这里你有我的动画功能:

func animateView() {

        let circle = viewProgress // viewProgress is a UIView

        var progressCircle = CAShapeLayer()

        let circlePath = UIBezierPath(arcCenter: circle.center, radius: circle.bounds.midX, startAngle: -CGFloat(M_PI_2), endAngle: CGFloat(3.0 * M_PI_2), clockwise: true)

        progressCircle = CAShapeLayer ()
        progressCircle.path = circlePath.CGPath
        progressCircle.strokeColor = UIColor.whiteColor().CGColor
        progressCircle.fillColor = UIColor.clearColor().CGColor
        progressCircle.lineWidth = 10.0

        circle.layer.addSublayer(progressCircle)

        let animation = CABasicAnimation(keyPath: "strokeEnd")
        animation.fromValue = 0
        animation.toValue = 0.4
        animation.duration = 1
        animation.fillMode = kCAFillModeForwards
        animation.removedOnCompletion = false

        progressCircle.addAnimation(animation, forKey: "ani") …
Run Code Online (Sandbox Code Playgroud)

position cabasicanimation cashapelayer ios swift

0
推荐指数
1
解决办法
2907
查看次数

CAShapeLayer 到 UILabel 隐藏文本

我正在尝试添加一个CAShapeLayerUILabel.
问题是:这样做后文本消失了:

let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.fillColor = UIColor.yellow.cgColor
item.layer.addSublayer(shapeLayer)
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以做到这一点?

cashapelayer ios swift

0
推荐指数
1
解决办法
1047
查看次数

UIBezierPath填充vs CAShapeLayer

我试图绘制一个简单的形状作为游戏界面的一部分,大多数教程使用UIBezierPath来定义CAShapeLayer的路径.但是,我注意到UIBezierPath已经有了一个现有的填充功能.

这可能是一个愚蠢的问题,但使用它和使用单独的CAShapeLayer绘制路径有什么区别?还有哪一个更好?

cashapelayer ios uibezierpath swift

0
推荐指数
1
解决办法
531
查看次数

iOS变形播放/暂停动画

我想在我正在开发的应用程序中使用像变形播放/暂停动画这样的YouTube.它看起来像这里的动画.我不知道该怎么做.我知道我需要一个CABasicAnimationCAShapeLayer.我知道我需要两个(或者更多?)- UIBezierPaths.一个将是动画shapeLayer的另一个路径toValue.就像是:

shape.path = pausePath().cgPath

let animation = CABasicAnimation(keyPath: "path")
animation.toValue = trainglePath().cgPath
animation.duration = 1
shape.add(animation, forKey: animation.keyPath)
Run Code Online (Sandbox Code Playgroud)

但我不知道如何走这条道路.我有以下问题:

  1. 道路的重要方式是什么?例如,从左到右绘制的线是否与从右到左制作的线不同?我的意思是path.move(to: startPoint); path.addLine(to: endPoint1)反对path.move(to: endPoint1);path.addLine(to: startPoint)
  2. 两条路是否足够?一个用于启动按钮,一个用于暂停按钮?如果是的话 - 绘制它们的正确方法是什么,以便它们能够"正确"地制作动画?我不在这里要求代码(不是我介意将代码作为答案)但是一些一般的解释对于初学者来说已经足够了.

cabasicanimation cashapelayer ios swift

0
推荐指数
2
解决办法
542
查看次数

如何在 Swift 中使用 CAShapeLayer 为 CALayer.SubLayer 创建橡皮擦

我一直在到处寻找一个被问过很多次的问题的答案。我花了几个小时浏览 SO 和 Google。必须有一个不需要费力搬山的答案。

我正在开发矢量绘图应用程序,最终使绘图和撤消功能正常工作。现在我需要一个橡皮擦:-o

编辑:根据@DonMag 的精彩文章,我能够非常接近橡皮擦,但仍然有些不对劲。因此,我将尝试解释我的视图和图层在应用程序中的位置以及我这样做的原因:

从底部视图/层开始到顶部......

  1. BackgroundImageView - 我使用这个图像视图来保存绘图表面的“背景”。它是一个可以更改的层,可以保存和调用新的“模板”。我把它分开,这样用户就不能擦除绘图表面。背景由 CAShapeLayers 组成,用于表示不同的纸张类型。

  2. MainImageView - 我使用这个图像视图来完成用户启动的所有绘图。所以我触摸并拖动我的手指,新的 CAShapeLayer 被添加到图像视图中。这使用户的绘图与“绘图表面”分开。这也是我希望擦除发生的地方

  3. PageImagesView - 我使用这个视图来保存用户可以添加到页面的图像,并移动/调整它们的大小。我不希望橡皮擦影响图像,但是如果 MainImageView 中绘制的一条线穿过图像并需要擦除,它应该让图像显示出来,而不是删除图像的某些部分。

我还添加了另一个图层,试图让橡皮擦工作,并将其称为“EraserImageView”,并将“蒙版”绘制到其中,然后尝试将该蒙版应用于 MainImageView。

这是我的绘图代码,每次调用 touchesMoved 时都会调用:

编辑: 将橡皮擦代码添加到我的绘图代码中。

 if eraser {
            let linePath = UIBezierPath()

            for (index, point) in line.enumerated() {
                if index == 0 {
                    midPoint = CGPoint(
                        x: (point.x + point.x) / 2,
                        y: (point.y + point.y) / 2
                    )
                    linePath.move(to: midPoint!)
                } else {
                    midPoint = CGPoint(
                        x: (point.x + line[index - 1].x) / …
Run Code Online (Sandbox Code Playgroud)

calayer cashapelayer ios swift eraser

0
推荐指数
1
解决办法
1906
查看次数