尝试在 Swift 中使用 AutoLayout 将两个视图卷曲 up/curl
Trying to curl up/curl down with two views using AutoLayout in Swift
使用自动布局,我将两个 UIView 在屏幕上居中,现在我正在尝试使用 CurlUp 从一个过渡到另一个,然后使用此功能进行 CurlDown:
func curlUp() {
let transitionOptions = UIViewAnimationOptions.TransitionCurlUp
UIView.transitionFromView(cardFront,
toView: cardBack,
duration: 5.0,
options: transitionOptions,
completion: { _ in
let transitionOptions = UIViewAnimationOptions.TransitionCurlDown
UIView.transitionFromView(self.cardBack,
toView: self.cardFront,
duration: 5.0,
options: transitionOptions,
completion: { _ in
//
})
})
}
发生的情况是整个屏幕卷曲,如图所示:
视图控制器的完整源代码在这里:
https://github.com/melling/Swift/blob/master/TransitionWithView/TransitionWithView/ViewController.swift
Github 项目使用 Xcode 6.3.1
构建和运行
这是用于添加子视图的代码:
func buildView() {
cardBack = UIView()
cardBack.setTranslatesAutoresizingMaskIntoConstraints(false)
cardBack.backgroundColor = UIColor.redColor()
self.view.addSubview(cardBack)
cardFront = UIView()
cardFront.setTranslatesAutoresizingMaskIntoConstraints(false)
cardFront.backgroundColor = UIColor.greenColor()
self.view.addSubview(cardFront)
let height = 50
let width = 50
let viewDictionary:Dictionary<String,UIView> = ["cardFront": cardFront, "cardBack": cardBack]
let metrics:Dictionary<String,Int> = ["width": width, "height": height]
let hConstraint = "H:[cardFront(==width)]"
let vConstraint = "V:[cardFront(==height)]"
addStandardConstraints(hConstraint, viewDictionary: viewDictionary, metrics: metrics)
addStandardConstraints(vConstraint, viewDictionary: viewDictionary, metrics: metrics)
centerViewXY(self.view, child: cardFront)
let h1Constraint = "H:[cardBack(==width)]"
let v1Constraint = "V:[cardBack(==height)]"
addStandardConstraints(h1Constraint, viewDictionary: viewDictionary, metrics: metrics)
addStandardConstraints(v1Constraint, viewDictionary: viewDictionary, metrics: metrics)
centerViewXY(self.view, child: cardBack)
NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "curlUp", userInfo: nil, repeats: false)
}
func centerViewXY(parent: UIView, child: UIView) {
var constX = NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: parent, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
parent.addConstraint(constX)
var constY = NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: parent, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
parent.addConstraint(constY)
}
func addStandardConstraints(aConstraint:String, viewDictionary:Dictionary<String,UIView!>, metrics:Dictionary<String, Int>) {
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(aConstraint, options: nil, metrics: metrics, views: viewDictionary))
}
[更新]
创建容器成功。我将工作代码提交给 Github:https://github.com/melling/Swift
TransitionWithView 是 Xcode 项目。
是的,一个常见问题。您需要使用另一个视图来包含您正在转换的两个视图。参见 this other SO answer。
使用自动布局,我将两个 UIView 在屏幕上居中,现在我正在尝试使用 CurlUp 从一个过渡到另一个,然后使用此功能进行 CurlDown:
func curlUp() {
let transitionOptions = UIViewAnimationOptions.TransitionCurlUp
UIView.transitionFromView(cardFront,
toView: cardBack,
duration: 5.0,
options: transitionOptions,
completion: { _ in
let transitionOptions = UIViewAnimationOptions.TransitionCurlDown
UIView.transitionFromView(self.cardBack,
toView: self.cardFront,
duration: 5.0,
options: transitionOptions,
completion: { _ in
//
})
})
}
发生的情况是整个屏幕卷曲,如图所示:
视图控制器的完整源代码在这里: https://github.com/melling/Swift/blob/master/TransitionWithView/TransitionWithView/ViewController.swift
Github 项目使用 Xcode 6.3.1
构建和运行这是用于添加子视图的代码:
func buildView() {
cardBack = UIView()
cardBack.setTranslatesAutoresizingMaskIntoConstraints(false)
cardBack.backgroundColor = UIColor.redColor()
self.view.addSubview(cardBack)
cardFront = UIView()
cardFront.setTranslatesAutoresizingMaskIntoConstraints(false)
cardFront.backgroundColor = UIColor.greenColor()
self.view.addSubview(cardFront)
let height = 50
let width = 50
let viewDictionary:Dictionary<String,UIView> = ["cardFront": cardFront, "cardBack": cardBack]
let metrics:Dictionary<String,Int> = ["width": width, "height": height]
let hConstraint = "H:[cardFront(==width)]"
let vConstraint = "V:[cardFront(==height)]"
addStandardConstraints(hConstraint, viewDictionary: viewDictionary, metrics: metrics)
addStandardConstraints(vConstraint, viewDictionary: viewDictionary, metrics: metrics)
centerViewXY(self.view, child: cardFront)
let h1Constraint = "H:[cardBack(==width)]"
let v1Constraint = "V:[cardBack(==height)]"
addStandardConstraints(h1Constraint, viewDictionary: viewDictionary, metrics: metrics)
addStandardConstraints(v1Constraint, viewDictionary: viewDictionary, metrics: metrics)
centerViewXY(self.view, child: cardBack)
NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "curlUp", userInfo: nil, repeats: false)
}
func centerViewXY(parent: UIView, child: UIView) {
var constX = NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: parent, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
parent.addConstraint(constX)
var constY = NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: parent, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
parent.addConstraint(constY)
}
func addStandardConstraints(aConstraint:String, viewDictionary:Dictionary<String,UIView!>, metrics:Dictionary<String, Int>) {
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(aConstraint, options: nil, metrics: metrics, views: viewDictionary))
}
[更新]
创建容器成功。我将工作代码提交给 Github:https://github.com/melling/Swift
TransitionWithView 是 Xcode 项目。
是的,一个常见问题。您需要使用另一个视图来包含您正在转换的两个视图。参见 this other SO answer。