提问人:pfg2009 提问时间:10/27/2018 更新时间:10/27/2018 访问量:203
iOS LayoutKit 可变布局
iOS LayoutKit mutable layouts
问:
我最近一直在玩 LayoutKit,虽然该框架干净且功能相当强大,但我正在努力寻找正确的模式来实现可变的 UI 组件(例如跟踪用户评论上的实时 LIKE 计数的标签)。这是我的两个问题,它们实际上归结为相同的基本原则 - 如何更新 LayoutKit 生成的 UI?
具体说来。。。
场景 1:
获取实例化生成的实例的推荐方法是什么?UIView
Layout
在网站上列出的“Hello World”示例中,假设我想更改 .我能想到的唯一方法是从闭包中保留对它的引用(<<<<< == 我的编辑):UIImage
config
var myImageView: UIImageView? = nil // <<<<<
let image = SizeLayout<UIImageView>(width: 50, height: 50, config: { imageView in
imageView.image = UIImage(named: "earth.jpg")
myImageView = imageView // <<<<<
})
let label = LabelLayout(text: "Hello World!", alignment: .center)
let stack = StackLayout(
axis: .horizontal,
spacing: 4,
sublayouts: [image, label])
let insets = UIEdgeInsets(top: 4, left: 4, bottom: 4, right: 8)
let helloWorld = InsetLayout(insets: insets, sublayout: stack)
helloWorld.arrangement().makeViews(in: rootView)
myImageView.image = UIImage(named: "someOtherImage.jpg") // <<<<<
但是,这种方法在某些情况下会带来挑战,并且通常很笨拙。我错过了什么?
场景 2:
假设我想将嵌入在某个布局深处的标签从“短的东西”更改为“需要重新计算布局的非常非常长的东西”。
我能想到的两种解决这个问题的方法:
- 使用更新的信息重新创建布局,并重新创建整个 UI 树。然而,这似乎是一个疯狂的昂贵操作,只是为了更新标签的文本。
- 使用适当的 s 重新创建布局并遵循动画路线,而是立即应用更改:
viewReuseId
.
// Apply the initial layout.
before.arrangement(width: 350, height: 250).makeViews(in: rootView)
// Prepare the animation to the final layout.
after.arrangement(width: 350, height: 250).prepareAnimation(for: rootView, direction: .rightToLeft).apply()
这似乎也是一种非常昂贵的更改标签值的方法。再说一遍,我错过了什么?LayoutKit 只能用于不可变接口吗?
答: 暂无答案
评论