UIDynamicView
简介
如果您想快速创建视图而不为每个视图创建令人烦恼的 .xib 文件,请使用此库。
使用方法
只需创建动态视图并添加您希望使用的子视图。
import UIDynamicView
private func popFactsDialog() {
// build the dynamic view with all of the props
let dv = UIDynamicView()
dv.prepareView(parentView: view,
padding: 14,
margin: 14,
maxWidthPercentFromParent: 0.65)
dv.dropShadow(shadowRadius: 5.0)
// add the title
let topTitleProps = InitialLabelProps(text: "Teletubbies",
textAlignment: .center,
font: UIFont.systemFont(ofSize: 20, weight: .bold))
dv.addView(initialProps: topTitleProps)
// add the image
let imgProps = InitialUIImageViewProps(imageName: "tt",
widthPercentFromParent: 0.3,
tag: 99,
alignment: .center)
dv.addView(initialProps: imgProps)
// add the description
let descriptionProps = InitialLabelProps(text:
"""
Teletubbies is a British children's television series
created by Ragdoll Productions' Anne Wood and Andrew
Davenport for BBC.
""",
textAlignment: .left,
font: UIFont.systemFont(ofSize: 17)
)
dv.addView(initialProps: descriptionProps)
// add the footer buttons
let nextFactButton = InitialButtonProps(labelText: "Previous Fact",
alignment: .left,
tapSelector: #selector(onPreviousFactTap))
let nextBtn = InitialButtonProps(labelText: "Next Fact!",
alignment: .right,
tapSelector: #selector(onNextFactTap))
let footerStackViewProps = InitialStackViewProps(subviewsInitialPropsList: [nextFactButton, nextBtn])
dv.addView(initialProps: footerStackViewProps)
dv.attachView(parentView: view)
}
@objc func onPreviousFactTap() {
}
@objc func onNextFactTap() {
}
另一个示例
import UIDynamicView
private func popHelpDialog() {
// build the dynamic view with all of the props
dv = UIDynamicView()
dv.prepareView(parentView: view,
padding: 14,
margin: 14,
maxWidthPercentFromParent: 0.65)
dv.dropShadow(shadowRadius: 5.0)
// add the title
let topTitleProps = InitialLabelProps(text: "Help",
textAlignment: .center,
font: UIFont.systemFont(ofSize: 20, weight: .bold))
dv.addView(initialProps: topTitleProps)
// add the description
let descriptionProps = InitialLabelProps(text:
"""
Please watch the below video to understand how to use the app
""",
textAlignment: .left,
font: UIFont.systemFont(ofSize: 19)
)
dv.addView(initialProps: descriptionProps)
// add the youtube video
let videoProps = InitialYoutubeVideoProps(videoId: "BywDOO99Ia0",
widthPercentFromParent: 0.75,
alignment: .center)
dv.addView(initialProps: videoProps)
// add the footer button
let okBtnProps = InitialButtonProps(labelText: "OK",
alignment: .right,
tapSelector: #selector(onOkBtnTap))
dv.addView(initialProps: okBtnProps)
dv.attachView(parentView: view)
}
对话框包装器
您还可以使用对话框包装器来快速构建对话框(例如控制器连接示例)
import UIDynamicView
private var dialogWrapper: UIDialogWrapper!
private func popDialog() {
// prepare the dialog
dialogWrapper = UIDialogWrapper(parentView: view, margin: 20, maxWidthPercentFromParent: 0.6)
// set title and description
dialogWrapper.setTitle(text: "Controller Connection")
dialogWrapper.setTopDesription(text: """
NOTICE: iOS 13.0 is required to connect your controller via Bluetooth
- With your Xbox controller, long click on the Xbox and pairing buttons together, until the Xbox button will start flashing
- In your iPhone/iPad, go to Settings app -> tap on Bluetooth -> activate it
- Look for the Xbox Controller in the list of devices, click on it and approve the connection
— You're all set!
""", size: 16)
// add bottom description, youtube video and footer
dialogWrapper.setBottomDesription(text: "** UPDATED: 10.4.2020")
dialogWrapper.setYoutubeVideo(videoId: "wPV0QtYm-4o", widthPercentFromParent: 0.75)
dialogWrapper.setFooter(leftBtnText: "Cancel", rightBtnText: "Ok", leftBtnTapSelector: #selector(onLeftTap), rightBtnTapSelector: #selector(onRightTap))
// attach the view to it's parent
dialogWrapper.attachView(parentView: view)
}
@objc func onLeftTap() {
dialogWrapper.dismiss()
print("on left tap")
}
@objc func onRightTap() {
print("on right tap")
}
安装
UIDynamicView可通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中。
pod 'UIDynamicView'
作者
osApss, [email protected]
许可
UIDynamicView遵循MIT许可。请参阅LICENSE文件获取更多信息。