gpt4 book ai didi

ios - 快速翻转 View : issues with whole screen flipping + tap gesture only works twice

转载 作者:可可西里 更新时间:2023-11-01 01:42:45 25 4
gpt4 key购买 nike

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

  • 整个屏幕都在翻转,不仅仅是 View ,有没有办法只翻转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 时,点击根本不起作用。

最佳答案

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

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

关于ios - 快速翻转 View : issues with whole screen flipping + tap gesture only works twice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27749790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com