gpt4 book ai didi

swift - UIViewanimation trantitionfromView 使用 UIViewflip 动画

转载 作者:行者123 更新时间:2023-11-28 12:42:44 24 4
gpt4 key购买 nike

构建包含抽认卡的应用。他们肯定需要翻牌的能力。为此,我有一个 UIViewController 并且为了避免翻转整个 View ,我将我的 subview 实现到一个容器中。

我在名为frontViewbackView 的容器中声明了两个 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/

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