【问题标题】:iOS CALayer mask strange half pixel lineiOS CALayer mask 奇怪的半像素线
【发布时间】:2018-04-30 00:20:31
【问题描述】:

我需要通过所有文本层将透明弧从 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

我该如何解决这个问题?

【问题讨论】:

    标签: ios calayer pixel cabasicanimation


    【解决方案1】:

    做了一些小的改动,比如:

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

    动画的最后几秒是这样的:

    两条弧线相互错位。

    那么发生了什么变化:

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

    • 它的结束角为 0

    • 顺时针设置为假

    这就是你要找的吗?

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多