iOS CALayer面具奇怪的半像素线

时间:2018-04-30 00:20:31

标签: ios calayer pixel cabasicanimation

我需要通过所有文本层将透明弧从0切换到pi。 除了一件事之外,代码在上面工作正常。 我在执行CABasicAnimation屏蔽CALayer时没有小宽度线,在空闲状态下没有奇怪的行。

half pixel line image

无法理解为什么它会显示,因为理论上它根本不可见......

这是我的游乐场代码:

import UIKit
import PlaygroundSupport

let rootView = UIView(frame: CGRect(x: 0, y: 0,
                                    width: 200,
                                    height: 200))
rootView.backgroundColor = .gray

let uiview = UIButton(frame: CGRect(x: 0,
                                  y: 0,
                                  width: 200,
                                  height: 200))
uiview.setTitle("H", for: .normal)
uiview.titleLabel?.font = UIFont.boldSystemFont(ofSize: 200)
uiview.setTitleColor(.black, for: .normal)
uiview.backgroundColor = UIColor.green
uiview.layer.cornerRadius = 100
uiview.layer.masksToBounds = true
rootView.addSubview(uiview)

let center = CGPoint(x: uiview.bounds.width / 2,
                     y: uiview.bounds.height / 2)

let path = UIBezierPath(arcCenter: center,
                        radius: 80,
                        startAngle: 0,
                        endAngle: .pi,
                        clockwise: true)
path.append(UIBezierPath(arcCenter: center,
                         radius: 78,
                         startAngle: -1,
                         endAngle: .pi,
                         clockwise: true))
path.append(UIBezierPath(rect: uiview.bounds))

let maskLayer = CAShapeLayer()
maskLayer.frame = uiview.bounds
maskLayer.path = path.cgPath
maskLayer.backgroundColor = UIColor.clear.cgColor
maskLayer.fillRule = kCAFillRuleEvenOdd
maskLayer.lineWidth = 0

uiview.layer.mask = maskLayer

let rotation = CABasicAnimation(keyPath: "transform.rotation")
rotation.fromValue = 0.0
rotation.toValue = Double.pi * 2
rotation.duration = 10
rotation.repeatCount = 2
maskLayer.add(rotation, forKey: nil)


PlaygroundPage.current.liveView = rootView

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

进行一些小改动,如下:

path.append(UIBezierPath(arcCenter: center,
                         radius: 78,
                         startAngle: .pi,
                         endAngle: 0,
                         clockwise: false))

动画的最后几秒看起来像这样:

last few seconds of animation

两个弧彼此错位。

所以改变了:

  • 第二个圆弧的起始角度为

  • 它的结束角度为0

  • 顺时针设置为false

这就是你要找的东西吗?