UIViewanimation trantitionfromView 使用 UIViewflip 动画 [英] UIViewanimation trantitionfromView using UIViewflip animation

查看:19
本文介绍了UIViewanimation trantitionfromView 使用 UIViewflip 动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Building an app containing flashCards. They surely require the ability to flip a card. To do this i have a UIViewController and to avoid flipping the whole view I've implemented my subView into a container.

我在名为 frontViewbackView 的容器中声明了两个子视图.frontView 有红色背景和标签表示正面,而 backView 有蓝色背景和标签表示背面.

I have declared two subviews in the container named, frontView and backView. frontView got red background and label saying front while backView got a blue background and label saying back.

我已经声明了一个变量,所以我可以检查显示哪一侧:varshowFront = true 有一个调用以下函数的 UIButton 操作:

I have declared a variable so i can check which side is showened : var showingFront = true Got a UIButton action which calls the following func:

    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 the backgroundColor disappears with the labels and i can only see the container flipping?. All help is appreciated

推荐答案

试试这个方法:在故事板中创建一个 UIView(width = 90,height = 132)并将其更改为FlashCard"

try this way: in the storyboard create a UIView(width = 90, height = 132) and change it class to " FlashCard"

创建一个按钮来翻转抽认卡.

also create a button to flip the 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()
    }



}

这篇关于UIViewanimation trantitionfromView 使用 UIViewflip 动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆