在swift中翻转视图:全屏翻转问题+点按手势只能工作两次

时间:2015-01-02 23:09:21

标签: ios swift uiview uiviewcontroller uitapgesturerecognizer

我想创建一个简单的翻转效果,但我不明白我在这里遇到的问题:

  • 整个屏幕正在翻转,不仅是视图,还有办法翻转100,100平方吗?
  • 我只能翻转两次,然后水龙头不再工作了,你知道为什么吗?

    import UIKit
    class ViewController: UIViewController {
    
    var fromOneToTwo : Bool = true
    var view1 : UIImageView!
    var view2 : UIImageView!
    var tap : UITapGestureRecognizer!
    
    func handleTap(tap: UITapGestureRecognizer){
    
        println("1/fromOneToTwo \(fromOneToTwo)")
        var v1 = (fromOneToTwo ? self.view1 : self.view2)
        var v2 = (fromOneToTwo ? self.view2 : self.view1)
    
        UIView.transitionFromView(v1, toView: v2, duration: 0.5, options: (UIViewAnimationOptions.TransitionFlipFromLeft | UIViewAnimationOptions.CurveEaseInOut) ) { finished in
    
            self.fromOneToTwo = !self.fromOneToTwo
            println("2/fromOneToTwo \(self.fromOneToTwo) ")
        }
    }
    
    func createView( str:String)->UIImageView!{
        var img = UIImage(named: str)!
        var imgView = UIImageView(frame: CGRectMake(0,0, 200, 200))
        imgView.image = img
        imgView.sizeToFit()
        imgView.userInteractionEnabled = true
        imgView.addGestureRecognizer(tap)
        return imgView
    }
    
    required init(coder aDecoder:NSCoder){
        super.init(coder:aDecoder)
    
        tap = UITapGestureRecognizer(target: self, action: "handleTap:")
        view1 = createView("imageOnee.png")
        view2 = createView("imageTwo.png")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        view.addSubview(view1)
        view.addSubview(view2)
    
        view.backgroundColor = UIColor.redColor()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    

    }

此外,当我尝试在自定义UIView中添加整个代码,并将自定义实例myView添加到ViewController时,点击根本不起作用。

1 个答案:

答案 0 :(得分:2)

我做了这项工作,为containerview1创建了view2视图。然后将点击识别器添加到此容器视图中。

class ViewController: UIViewController {

    var fromOneToTwo : Bool = true
    var view1 : UIImageView!
    var view2 : UIImageView!
    var container : UIView!
    var tap : UITapGestureRecognizer!

    func handleTap(tap: UITapGestureRecognizer){

        println("1/fromOneToTwo \(fromOneToTwo)")
        var v1 = (fromOneToTwo ? self.view1 : self.view2)
        var v2 = (fromOneToTwo ? self.view2 : self.view1)

        UIView.transitionFromView(v1, toView: v2, duration: 0.5, options: .TransitionFlipFromLeft | .CurveEaseInOut ) { finished in

            self.fromOneToTwo = !self.fromOneToTwo
            println("2/fromOneToTwo \(self.fromOneToTwo) ")
        }
    }

    func createView( str:String)->UIImageView!{
        var img = UIImage(named: str)!
        var imgView = UIImageView(frame: CGRectMake(0, 0, 200, 200))
        imgView.image = img
        imgView.sizeToFit()
        return imgView
    }

    required init(coder aDecoder:NSCoder){
        super.init(coder:aDecoder)

        tap = UITapGestureRecognizer(target: self, action: "handleTap:")
        view1 = createView("imageOnee.png")
        container = UIView(frame: CGRectMake(0, 0, view1.bounds.width, view1.bounds.height))
        container.userInteractionEnabled = true

        container.addGestureRecognizer(tap)
        container.addSubview(view1)
        view2 = createView("imageTwo.png")
        container.addSubview(view2)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(container)

        view.backgroundColor = UIColor.redColor()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}