作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
构建包含抽认卡的应用。他们肯定需要翻牌的能力。为此,我有一个 UIViewController 并且为了避免翻转整个 View ,我将我的 subview 实现到一个容器中。
我在名为frontView
和backView
的容器中声明了两个 subview 。 frontView
有红色背景和标签表示前面,而 backView
有蓝色背景和标签表示后面。
我已经声明了一个变量,所以我可以检查显示的是哪一侧:var showingFront = true
得到了一个调用以下函数的 UIButton 操作:
if showingFront == true {
UIView.transitionFromView(forside, toView: bagside, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight, completion: nil)
showingFront = false
} else {
UIView.transitionFromView(bagside, toView: forside, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight, completion: nil)
showingFront = true
}
这会来回翻转 View ,但 backgroundColor 会随标签一起消失,我只能看到容器在翻转吗?感谢所有帮助
最佳答案
试试这个:在 Storyboard中创建一个 UIView(width = 90, height = 132)并将其更改为“FlashCard”
还创建一个按钮来翻转抽认卡。
抽认卡:
import UIKit
class ItemView: UIView {
var label:UILabel?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame:CGRect){
super.init(frame: frame)
}
convenience init(frame:CGRect, backgroundcolor:UIColor, labelText:String){
self.init(frame: frame)
self.backgroundColor = backgroundcolor
self.contentMode = .ScaleAspectFit
label = UILabel(frame: CGRectMake(0,0,50,50))
label!.textAlignment = NSTextAlignment.Center
label!.textColor = UIColor.whiteColor()
label?.center = self.center
label!.text = labelText
self.addSubview(label!)
}
}
class FlashCard: UIView {
var backView:ItemView?
var frontView:ItemView?
var isFrontView_CurrentlyVisable_onTheScreen = false
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.backgroundColor = UIColor.clearColor()
self.userInteractionEnabled=true
setupview()
}
override init(frame:CGRect){
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
self.userInteractionEnabled=true
setupview()
}
func setupview(){
loadFront()
loadBack()
}
func loadFront(){
if frontView == nil {
frontView = ItemView.init(frame: self.bounds, backgroundcolor: UIColor.redColor(), labelText: "Front")
self.addSubview(frontView!)
frontView?.hidden = true
}
}
func loadBack(){
if backView == nil {
backView = ItemView.init(frame: self.bounds, backgroundcolor: UIColor.blueColor(), labelText: "Back")
self.addSubview(backView!)
backView?.hidden = false
}
}
func unloadBack(){
backView?.removeFromSuperview()
backView=nil
}
func flip(){
let ObjectToDisplay: ItemView
let currentlyVisableObjectOnScreen: ItemView
if isFrontView_CurrentlyVisable_onTheScreen{
ObjectToDisplay = backView!
currentlyVisableObjectOnScreen = frontView!
isFrontView_CurrentlyVisable_onTheScreen = false
}else{
ObjectToDisplay = frontView!
currentlyVisableObjectOnScreen = backView!
isFrontView_CurrentlyVisable_onTheScreen = true
}
if ObjectToDisplay.hidden{
ObjectToDisplay.hidden = false
}
print("isFrontView_CurrentlyVisable_onTheScreen?: \(isFrontView_CurrentlyVisable_onTheScreen)")
UIView.transitionFromView(currentlyVisableObjectOnScreen, toView:ObjectToDisplay, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight, completion: {(done)->() in
if done{
currentlyVisableObjectOnScreen.hidden = true
}
})
}
}
在您的 View Controller 中:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myButton: UIButton!
@IBOutlet weak var card: FlashCard!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func buttonPressed(sender: AnyObject) {
card.flip()
}
}
关于swift - UIViewanimation trantitionfromView 使用 UIViewflip 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39043032/
构建包含抽认卡的应用。他们肯定需要翻牌的能力。为此,我有一个 UIViewController 并且为了避免翻转整个 View ,我将我的 subview 实现到一个容器中。 我在名为frontVie
我是一名优秀的程序员,十分优秀!