ios - 在 UIView 中居中 CAShapeLayer?

标签 ios swift cashapelayer

像这道题大概有5道以上,但都没有解决问题。

我想在 View 上放置一个圆圈。尽管通过类似的问题尝试了各种方法,但它并没有发生。

我不确定是否会发生这种情况,因为我设置了 translatesAutoresizingMaskIntoConstraints = false。我试着玩弄形状层的中心点和 anchor ,但疯狂的行为发生了。我尝试将这段代码放在 viewDidLayouSubviews 中。没用。我还能做什么?

colorSizeGuide 是我试图将图层居中的 View 。

func setupConstraints() {
    // other constraints set up here
    colorSizeGuide.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    colorSizeGuide.topAnchor.constraint(equalTo: view.topAnchor, constant: 30).isActive = true
    colorSizeGuide.widthAnchor.constraint(equalToConstant: 30).isActive = true
    colorSizeGuide.heightAnchor.constraint(equalToConstant: 30).isActive = true
}

func setupColorSizeGuide() {
    shape.path = UIBezierPath(ovalIn: colorSizeGuide.frame).cgPath
    shape.position = self.colorSizeGuide.center
    shape.anchorPoint = CGPoint(x: 0.5, y: 0.5)

    shape.strokeColor = (UIColor.black).cgColor
    shape.fillColor = (UIColor.clear).cgColor
    shape.lineWidth = 1.0
    colorSizeGuide.layer.addSublayer(shape)

} 

最佳答案

您可以执行几个选项。

  1. 如果您不在滚动环境中(例如 tableView),请在 View 上使用矩形宽度一半的角半径,然后 View 将被裁剪为圆形 - 这是不是特别快或可自定义,但它可以完成工作。

enter image description here

Playground 代码:

import UIKit
import PlaygroundSupport

let frame: CGRect = CGRect(x: 0, y: 0, width: 100, height: 100)
let view: UIView = UIView(frame: frame)
view.backgroundColor = .magenta
view.layer.cornerRadius = frame.size.width / 2
PlaygroundPage.current.liveView = view
  1. 您可以让您的自定义 UIView 子类实现 layerClass 方法并在那里返回 CAShapeLayer。这意味着您 View 的 self.layer 实际上是一个形状层。您可以在那里设置一个圆圈的路径,然后就可以了。如果您想将其置于 View 的中心,只需确保在定义 UIBezierPath 时使用 View 的边界坐标即可。

enter image description here

Playground 代码:

import UIKit
import PlaygroundSupport

let frame: CGRect = CGRect(x: 0, y: 0, width: 100, height: 100)
let view: UIView = UIView(frame: frame)
PlaygroundPage.current.liveView = view

class ShapeView: UIView {

    override class var layerClass: AnyClass { return CAShapeLayer.self }

    override init(frame: CGRect) {
        super.init(frame: frame)
        (layer as? CAShapeLayer)?.fillColor = UIColor.red.cgColor
        (layer as? CAShapeLayer)?.strokeColor = UIColor.green.cgColor
        (layer as? CAShapeLayer)?.path = UIBezierPath(ovalIn: frame).cgPath
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

let sv = ShapeView(frame: frame)
view.addSubview(sv)
  1. 通过执行 CAShapeLayer.init 创建一个非 View 形状层。将你的 View 放在它的 layoutSubviews 方法中(或者 viewDidLayoutSubviews 如果你在 View Controller 中这样做)并为形状层设置一个合适的框架,就像它是一个 View 。

enter image description here

Playground 代码:

import UIKit
import PlaygroundSupport

let frame: CGRect = CGRect(x: 0, y: 0, width: 100, height: 100)
let smallFrame: CGRect = CGRect(x: 0, y: 0, width: 10, height: 10)
let view: UIView = UIView(frame: frame)
view.backgroundColor = .magenta
PlaygroundPage.current.liveView = view



let sl = CAShapeLayer()
sl.path = UIBezierPath(ovalIn: smallFrame).cgPath
sl.fillColor = UIColor.red.cgColor
sl.strokeColor = UIColor.green.cgColor

sl.frame.origin.x = 30
sl.frame.origin.y = 30

view.layer.addSublayer(sl)

关于ios - 在 UIView 中居中 CAShapeLayer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41158306/

相关文章:

ios - 使用 CAShapeLayer 屏蔽呈现 View Controller 也屏蔽呈现 View Controller

ios - 如何在 iOS 应用程序中加载初始 UIViewController?

ios - Swift 如何使用枚举获取字符串值

ios - 在应用程序退出时保存变量

swift - 禁止在 Swift 中设置特定值

ios - 如何在 ios objective-c 中使用 NSAttributedString 在多行标签中添加背景线

iphone - 如何在Objective-C中更改CAShapeLayer相交部分的颜色?

ios - 使用 2 个 UIButton 模拟 UISegmentedControl

ios - UITableViewCell 的高度不起作用

iOS Safari 使用错误的域作为相对 CSS 路径