我正在尝试使用CAShapeLayer绘制圆形按钮并为其设置动画,但是仅该绘制让我很头疼-我似乎无法弄清楚如何将数据传递到类中。
这是我的设置:-会绘制CAShapeLayer的UIView类型的类-该视图在我的视图控制器中呈现,并使用自动布局约束构建
我尝试使用layoutIfNeeded,但似乎传递数据的时间太晚,无法绘制视图。我也尝试过在vieWillLayoutSubviews()中重画视图,但是什么也没有。下面的示例代码。我究竟做错了什么?
我是否太早/太迟地传递数据?我绘制bezierPath太晚了吗?
我非常感谢指针。
也许还有第二个后续问题:是否有一种更简单的方法来绘制与其视图大小绑定的圆形路径?
在我的View Controller中:
import UIKit
class ViewController: UIViewController {
let buttonView: CircleButton = {
let view = CircleButton()
view.backgroundColor = .black
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewWillLayoutSubviews() {
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(buttonView)
buttonView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
buttonView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
buttonView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.75).isActive = true
buttonView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25).isActive = true
buttonView.layoutIfNeeded()
buttonView.arcCenter = buttonView.center
buttonView.radius = buttonView.frame.width/2
}
override func viewDidAppear(_ animated: Bool) {
print(buttonView.arcCenter)
print(buttonView.radius)
} …Run Code Online (Sandbox Code Playgroud) 我已经为节标题创建了一个类,并将其加载到我的 UICollectionView 中。我能够显示第一个标题(尽管很奇怪,请参见下文),但是以下任何部分标题都是空白的。正在引用尺寸,但内容(背景颜色、标签)不会出现。
然后还有...确实显示的一个节标题,它以大约缩进显示。150px 无明显原因。标题默认居中对齐吗?如果是这样,我将如何左对齐它们?
我的节标题类:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
let section = indexPath.section
switch section {
case 0:
let tagsHeader = searchCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "TagsHeaderView", for: indexPath) as! SectionHeaderView
tagsHeader.headerString = "Recent Tags"
tagsHeader.backgroundColor = .green
return tagsHeader
default:
let tagsHeader = searchCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "TypeHeaderView", for: indexPath) as! SectionHeaderView
tagsHeader.headerString = "Type"
tagsHeader.backgroundColor = .blue
return tagsHeader
}
default:
return UICollectionReusableView()
}
}
Run Code Online (Sandbox Code Playgroud)
在我的 …