UIViewanimation trantitionfromView 使用 UIViewflip 动画

UIViewanimation trantitionfromView using UIViewflip animation

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

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

    }

这会来回翻转视图,但背景颜色会随标签一起消失,我只能看到容器在翻转吗?感谢所有帮助

试试这个方法: 在情节提要中创建一个 UIView(width = 90, height = 132) 并将其 class 更改为“ 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
            }
        })
    }


}

在你的 viewcontroller:

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()
    }



}