UIViewanimation trantitionfromView using UIViewflip animation

Building an application containing flashCards. They certainly require the ability to flip the card. For this, I have a UIViewController and, in order not to flip the whole view, I embedded my subView in the container.

I declared two subqueries in a container named frontViewand backView. frontViewreceived a red background and a label speaking in front, and backViewreceived a blue background and a label speaking in front .

I declared a variable, so I can check which side is highlighted: var showingFront = trueReceived a UIButton action that calls the following function:

    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

    }

This flips the view back and forth, but does backgroundColor disappear with shortcuts, and I can only see the container overflow ?. All help is appreciated.

0
1

: UIView ( = 90, = 132) " FlashCard"

, .

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


}

:

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



}
+2

Source: https://habr.com/ru/post/1656670/


All Articles