快速翻转视图:整个屏幕翻转+点击手势的问题只工作两次

Posted

技术标签:

【中文标题】快速翻转视图:整个屏幕翻转+点击手势的问题只工作两次【英文标题】:flipping view in swift : issues with whole screen flipping + tap gesture only works twice 【发布时间】:2015-01-02 23:09:21 【问题描述】:

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

整个屏幕都在翻转,不只是视图,有没有办法只翻转100100平方?

我只能翻两下,然后水龙头就不能用了,你知道为什么吗?

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】:

我为view1view2 创建了一个container 视图。然后将点击识别器添加到此容器视图中。

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

【讨论】:

以上是关于快速翻转视图:整个屏幕翻转+点击手势的问题只工作两次的主要内容,如果未能解决你的问题,请参考以下文章

只翻转一个方向

Android ViewFlipper + 手势检测器

在 iPad 中呈现模式视图翻转视图

在不翻转标签的情况下翻转视图

我想翻转我的屏幕[关闭]

Android测试的坑——屏幕翻转