我正在尝试使用UIView.transitionFromView在Playground中的两个视图之间进行简单的翻转动画。如此question中所述,旧版Xcode只需选择“在完全模拟器中运行”选项,但平台<下的操场设置中不再提供该选项/ strong>适用于Xcode 7.1。
在我的工作示例中,始终会立即显示底部视图。执行转换,但使用任何动画:
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,这样我就可以对其他一些更改进行原型设计。通过注释** flipFrontToBack()**方法,您可以看到未翻录和“翻转”的图像。所以,我得到了翻转,但没有动画。
答案 0 :(得分:2)
我取得了一些进展。我不得不使用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