以声明性方式创建视图层次结构。
快速查看
view.pd.add(
imageView.pd.image(logoImage),
label.pd.text("Logo").textColor(.red).font(size: 20),
button.pd.title("Go").action(buttonTapped)
)
它比指令性API更短更简洁,并且比storyboards更容易编辑。
安装
Carthage
github "wordlessj/Panda" ~> 2.0
CocoaPods
pod 'Panda', '~> 2.0'
用法
基础知识
所有 UIKit 和 Core Animation 中的可设置属性和设置方法都在 pd
扩展中提供,去掉了 set
前缀。
视图配置并添加到父视图,就像你在 快速查看 中看到的一样,您也可以嵌套 add()
来创建更复杂的层级,注意 pd
的配置是可选的。
view.pd.add(
containerView.pd.clipsToBounds(true).add(
view1.pd.backgroundColor(.red)
),
view2
)
还可以添加其他类型的 add*()
方法,如 UIGestureRecognizer
和 UILayoutGuide
。不要将它们全部放在一个 add()
方法中,推荐进行分组。
view.pd.add(
// view hierarchy
).add(
layoutGuide.pd.identifier("center guide")
).add(
tapGestureRecognizer.pd.numberOfTapsRequired(2).action(doubleTapped)
)
如果方法是可接受 *Convertible
的,可以将对象直接配置并传递给方法。
// mask() accepts a UIViewConvertible.
// You can pass a plain or configured UIView.
view.pd.mask(
maskView.pd.backgroundColor(.white)
)
添加和删除
add()
和 remove()
方法在以下类型中可用,接着是可以添加的类型。remove()
主要用于一次性删除多个对象而不进行配置。
类型 | 可以添加的类型 |
---|---|
CALayer |
CALayer 、CAAnimation |
UIAlertController |
、 |
UIDynamicAnimator |
UIDynamicBehavior |
UIDynamicBehavior |
UIDynamicBehavior |
UIView |
UIView 、UIGestureRecognizer 、UIMotionEffect 、UILayoutGuide 、UIInteraction |
UIViewController |
UIViewController 、UIKeyCommand |
使用
addArrangedSubview()
将UIView
添加到UIStackView
。
自定义属性
使用智能键路径,您可以使用 set()
来设置不在 Panda 中找到的自定义属性。
customView.pd.set(\.flashes, true)
操作
目标操作已替换为带有闭包的 action()
。它们适用于以下类型。
CADisplayLink
,初始化器代替action()
。UIAccessibilityCustomAction
UIBarButtonItem
UIControl
UIGestureRecognizer
字体
UIFont 工厂方法可以直接通过以下方法使用。
font(style:
) font(style:
, compatibleWith: ) font(size:
) font(size:
, weight: ) font(boldSize:
) font(italicSize:
) font(monospacedDigitSize:
, weight: )
它们适用于以下类型。
UIButton
,设置titleLabel
的字体。UILabel
UISimpleTextPrintFormatter
UITextField
UITextView
变换
有几个方便的方法可以设置 CollectionsViewLayoutAttributes
和 UIView
的变换。每个变换方法都有相应的 concat*()
方法将变换追加到现有的变换中。
rotation(radian:
) rotation(degree:
) scale(x:
, y: ) scale(
) translation(x:
, y: )
控件状态与栏度量
对于接受 UIControlState
或 UIBarMetrics
作为参数的方法,如 title(_:for:)
,存在便利方法用于设置所有状态或度量,如 title(_:highlighted:selected:disabled:)
,第一个参数用于正常状态,其他参数是可选的。
组合
为了方便,有几个组合方法可以同时设置多个属性。
类型 | 组合 | 组件 |
---|---|---|
CALayer |
边框 |
borderWidth 、borderColor |
CALayer |
阴影 |
shadowOpacity 、shadowRadius 、shadowOffset 、shadowColor 、shadowPath |
UICollectionView 、UIPageViewController 、UIPickerView 、UITableView |
sourceDelegate |
dataSource 、delegate |
|
阴影 |
shadowColor 、shadowOffset |
|
alwaysBounce |
alwaysBounceHorizontal 、alwaysBounceVertical |
|
showsScrollIndicators |
showsHorizontalScrollIndicator 、showsVerticalScrollIndicator |
UIView |
边框 |
layer.border |
UIView |
cornerRadius |
layer.cornerRadius 、layer.masksToBounds 设置为 true |
UIView |
shouldRasterize |
layer.shouldRasterize 、layer.rasterizationScale 设置为屏幕比例 |
UIView |
阴影 |
layer.shadow |
设置类方法
有些方法不以 set
开头,但通常在配置时调用,它们也可以使用。
UIAlertController.pd.addTextField(configure:=)
UIGestureRecognizer.pd.require(toFail:=)
注册和挂起
对于 UICollectionView
和 UITableView
,在注册和挂起单元格和视图时需要标识符,通常需要将特定类型强制转换。实际上,一个标识符关联着一个特定类型,因此可以使用类型本身而不是标识符。
collectionView.pd.register(CustomCell.self)
let cell: CustomCell = collectionView.pd.dequeue(CustomCell.self, for: indexPath)
重用
如果多个对象共享相似的配置,或者你想创建类似CSS的东西,可以将配置提取到方法中,然后使用 do()
应用该方法。
view.pd.add(
firstLabel.pd.do(configLabel),
secondLabel.pd.do(configLabel)
)
func configLabel(_ label: UILabel) {
label.pd.textColor(.red).font(size: 20).numberOfLines(0)
}
竹子
竹子 是一个框架,可以将 Auto Layout 和手动布局合并为一行,与 Panda 一起使用,使得在代码中创建视图变得极其简单和容易。
许可证
Panda 采用 MIT 许可证发布。请参阅 LICENSE 获取详细信息。