swift 中的翻转视图:整个屏幕翻转的问题 + 点击手势只能工作两次
flipping view in swift : issues with whole screen flipping + tap gesture only works twice
我想创建一个简单的翻转效果,但我不明白我这里遇到的问题:
- 整个屏幕都在翻转,不只是视图,有没有办法只翻转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 时,点击根本不起作用。
我完成了这项工作,为 view1
和 view2
创建了一个 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()
}
}
我想创建一个简单的翻转效果,但我不明白我这里遇到的问题:
- 整个屏幕都在翻转,不只是视图,有没有办法只翻转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 时,点击根本不起作用。
我完成了这项工作,为 view1
和 view2
创建了一个 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()
}
}