我正在尝试使用 UIView.transitionFromView 在 Playground 中的两个 View 之间制作一个简单的翻转动画。如本 question 中所述,旧版本的 Xcode 只需要选择“在完整模拟器中运行”选项,但在 Xcode 7.1 的 Platform 下的 Playground Settings 中不再可用。
在我的工作示例中,底 View 总是立即显示。执行过渡,但使用任何动画:
import UIKit
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
func flipFrontToBack(frontView: UIView, backView: UIView) {
let transitionOptions = UIViewAnimationOptions.TransitionFlipFromLeft
var views : (frontView: UIView, backView: UIView)
views = (frontView: frontView, backView: backView)
UIView.transitionFromView(views.frontView,
toView: views.backView,
duration: 2.0,
options: transitionOptions,
completion: { _ in
})
}
func smallSquare(backgroundColor: UIColor, borderColor: UIColor) -> UIView {
let bounds = CGRect(x: 50, y: 50, width: 50, height: 50)
let view = UIView(frame: bounds)
view.backgroundColor = backgroundColor
view.layer.borderColor = borderColor.CGColor
view.layer.borderWidth = 5
view.layer.cornerRadius = 10
return view
}
func makeView() -> UIView {
let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
let view = UIView(frame: bounds)
view.backgroundColor = UIColor.lightGrayColor()
view.layer.borderColor = UIColor.purpleColor().CGColor
view.layer.borderWidth = 5
view.layer.cornerRadius = 10
return view
}
let view = makeView()
var frontCard = smallSquare(UIColor.blackColor(), borderColor: UIColor.whiteColor())
var backCard = smallSquare(UIColor.whiteColor(), borderColor: UIColor.redColor())
view.addSubview(backCard)
view.addSubview(frontCard)
XCPlaygroundPage.currentPage.liveView = view
flipFrontToBack(frontCard, backView: backCard)
我的代码在我的 Xcode 项目中运行。我只是想将它迁移到 Playground,这样我就可以对一些额外的更改进行原型(prototype)设计。通过注释掉 ** flipFrontToBack()** 方法,您可以看到未翻转和“翻转”的图像。所以,我得到了翻转,但没有动画。
最佳答案
我取得了一点进步。我必须使用 NSTimer 来安排翻转至少 0.1 秒。容器翻转,而不是两张牌,但这是另一个问题。我还重构了一个类,因为一旦你超越几行,它们似乎更容易使用。无论如何,一些“工作”代码:
import UIKit
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
class MyView: UIView {
var frontCard:UIView!
var backCard:UIView!
func makeView() {
self.backgroundColor = UIColor.lightGrayColor()
self.layer.borderColor = UIColor.purpleColor().CGColor
self.layer.borderWidth = 5
self.layer.cornerRadius = 10
}
func flipFrontToBack(frontView: UIView, backView: UIView) {
let transitionOptions = UIViewAnimationOptions. TransitionFlipFromLeft
var views : (frontView: UIView, backView: UIView)
views = (frontView: frontView, backView: backView)
/*
http://stackoverflow.com/questions/24413539/uiview- transitionwithview-swift-syntax
*/
UIView.transitionFromView(views.frontView,
toView: views.backView,
duration: 2.0,
options: transitionOptions,
completion: { _ in
})
}
func smallSquare(backgroundColor: UIColor, borderColor: UIColor) -> UIView {
let bounds = CGRect(x: 50, y: 50, width: 50, height: 50)
let view = UIView(frame: bounds)
view.backgroundColor = backgroundColor
view.layer.borderColor = borderColor.CGColor
view.layer.borderWidth = 5
view.layer.cornerRadius = 10
return view
}
override init(frame: CGRect) {
super.init(frame: frame)
frontCard = smallSquare(UIColor.blackColor(), borderColor: UIColor.whiteColor())
backCard = smallSquare(UIColor.whiteColor(), borderColor: UIColor.redColor())
makeView()
self.addSubview(backCard)
self.addSubview(frontCard)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func update() {
flipFrontToBack(frontCard, backView: backCard)
}
func flipIt() {
NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "update", userInfo: nil, repeats: false)
}
}
let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
let view = MyView(frame: bounds)
XCPlaygroundPage.currentPage.liveView = view
view.flipIt() // Works if NSTimer > 0
//view.update() // Doesn't work. Flips immediately
关于ios - Xcode 7.1 中没有 "Run in Full Simulator"的 UIView 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33338225/