gpt4 book ai didi

ios - 尝试在 Swift 中使用 AutoLayout curl / curl 两个 View

转载 作者:行者123 更新时间:2023-11-29 02:04:36 25 4
gpt4 key购买 nike

使用自动布局,我在屏幕上居中了两个 UIView,现在我尝试使用 CurlUp 从一个 View 转换到另一个 View ,然后使用此函数使用 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
//
})

})

}

发生的情况是整个屏幕 curl ,如下图所示:

enter image description here

View Controller 的完整源代码在这里: https://github.com/melling/Swift/blob/master/TransitionWithView/TransitionWithView/ViewController.swift

Github 项目使用 Xcode 6.3.1 构建并运行

这是用于添加 subview 的代码:

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 项目。

enter image description here

最佳答案

是的,一个常见问题。您需要使用另一个 View 来包含您正在转换的两个 View 。参见 this other SO answer .

关于ios - 尝试在 Swift 中使用 AutoLayout curl / curl 两个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29923061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com