翻转动画在 swift 中的工作原理 2
How the flip animation works in swift 2
我有一个 UIView
,在这个视图中我有 UIImageView
。我在 UIView
里面还有一个按钮。我想要做的是当我点击这个按钮时我想制作一个翻转动画并删除我的 UIImageView
并将另一个视图加载到这个超级视图中。在我的按钮中点击甚至我做了这样的事情
func shareClick()
{
print("SHARE CLICK")
if showingBack {
UIView.transitionWithView(shareView, duration: 1.0, options: .TransitionFlipFromRight, animations: {
self.imgVwTop.removeFromSuperview()
}, completion: nil)
showingBack=false
}
else
{
UIView.transitionWithView(imgVwTop, duration: 1.0, options: .TransitionFlipFromRight, animations: {
self.shareView.removeFromSuperview()
}, completion: nil)
showingBack=true
}
}
我对这种行为感到困惑,不明白具体该怎么做。这个按钮点击事件在这里什么都不做。
这段代码有效...
self.debug.hidden = true
let image = UIImage(data: data!)
self.debug.image = image
if (swipe.direction == UISwipeGestureRecognizerDirection.Left) {
UIView.transitionWithView(self.debug, duration: 1.0, options: [.TransitionFlipFromRight], animations: {
self.debug.hidden = false
}, completion: { _ in })
}
我在隐藏后将新图像加载到我的图像中。
看来您的想法是正确的,我相信您已经正确设置了要在某种容器中翻转的视图。
我认为如果您以编程方式实例化两个视图(在您的情况下是 UIImageView 和 Button),将会有所帮助。当您转换时,另一个视图将被卸载,因为它们被列为弱视图,因此最好即时创建它们。
我做了一个视图控制器来测试这个想法,最初 currentView 将从情节提要中实例化,然后以编程方式创建,每次按下按钮时都会创建一个新视图来替换另一种,执行动画并将新视图设置为下一次按下按钮时的currentView。
class ViewController: UIViewController {
@IBOutlet weak var currentView: UIView!
@IBOutlet weak var container: UIView!
var flipped = false
@IBAction func buttonPressed(sender: AnyObject) {
let new: UIView!
if flipped {
new = UIImageView(frame: container.bounds)
new.backgroundColor = UIColor.redColor()
flipped = false
}
else {
new = UIButton(frame: container.bounds)
new.backgroundColor = UIColor.blueColor()
flipped = true
}
let options: UIViewAnimationOptions = [.TransitionFlipFromLeft, .AllowUserInteraction, .BeginFromCurrentState]
UIView.transitionFromView(currentView, toView: new, duration: 0.5, options: options, completion: nil)
self.currentView = new
}
}
希望这对您有所帮助:)
您可以执行以下操作(我假设 share view
包含图像视图和按钮):
override func viewDidLoad()
{
super.viewDidLoad()
shareView = UIView(frame: CGRectMake(30, 100, 300, 400)) //set the frame of the holder view
flippedView = UIView(frame: shareView!.bounds) //setup flipped view
flippedView!.backgroundColor = UIColor.redColor() //for test
isFlipped = false //initially not flipped
//set up the initial view with image and button
aImageView = UIImageView(frame: shareView!.bounds)
aImageView!.image = UIImage(named: "1.jpg")
shareButton = UIButton(type: .System)
shareButton!.setTitle("share", forState: .Normal)
shareButton!.frame = CGRectMake(0, 0, 150, 50)
shareButton!.addTarget(self, action: "shareButtonAction", forControlEvents: .TouchUpInside)
//add both imageview and button to holder view
shareView!.addSubview(aImageView!)
shareView!.addSubview(shareButton!)
//finally add holder to self view
self.view.addSubview(shareView!)
}
这里,使用transitionWithView
方法是不能去掉image view的super view的。您能做的最好的事情就是用翻转后要显示的新视图替换图像视图。再一次,您可以通过将其添加为子视图来翻转回图像视图。例如:
func shareButtonAction()
{
if (self.isFlipped! == false)
{
UIView.transitionWithView(shareView!, duration: 0.5, options:.TransitionFlipFromRight, animations: { () -> Void in
// self.aImageView!.image = UIImage(named: "2.jpg")
//hear remove the imageview add new view, say flipped view
self.aImageView!.removeFromSuperview()
self.shareView!.addSubview(self.flippedView!)
}, completion: { (Bool) -> Void in
self.isFlipped! = true
self.shareView!.bringSubviewToFront(self.shareButton!) //button should be top of the holder view
})
}
else
{
UIView.transitionWithView(shareView!, duration: 0.5, options:.TransitionFlipFromRight, animations: { () -> Void in
//move back, remove flipped view and add the image view
self.flippedView!.removeFromSuperview()
self.shareView!.addSubview(self.aImageView!)
}, completion: { (Bool) -> Void in
self.isFlipped! = false
self.shareView!.bringSubviewToFront(self.shareButton!)
})
}
}
我有一个 UIView
,在这个视图中我有 UIImageView
。我在 UIView
里面还有一个按钮。我想要做的是当我点击这个按钮时我想制作一个翻转动画并删除我的 UIImageView
并将另一个视图加载到这个超级视图中。在我的按钮中点击甚至我做了这样的事情
func shareClick()
{
print("SHARE CLICK")
if showingBack {
UIView.transitionWithView(shareView, duration: 1.0, options: .TransitionFlipFromRight, animations: {
self.imgVwTop.removeFromSuperview()
}, completion: nil)
showingBack=false
}
else
{
UIView.transitionWithView(imgVwTop, duration: 1.0, options: .TransitionFlipFromRight, animations: {
self.shareView.removeFromSuperview()
}, completion: nil)
showingBack=true
}
}
我对这种行为感到困惑,不明白具体该怎么做。这个按钮点击事件在这里什么都不做。
这段代码有效...
self.debug.hidden = true
let image = UIImage(data: data!)
self.debug.image = image
if (swipe.direction == UISwipeGestureRecognizerDirection.Left) {
UIView.transitionWithView(self.debug, duration: 1.0, options: [.TransitionFlipFromRight], animations: {
self.debug.hidden = false
}, completion: { _ in })
}
我在隐藏后将新图像加载到我的图像中。
看来您的想法是正确的,我相信您已经正确设置了要在某种容器中翻转的视图。
我认为如果您以编程方式实例化两个视图(在您的情况下是 UIImageView 和 Button),将会有所帮助。当您转换时,另一个视图将被卸载,因为它们被列为弱视图,因此最好即时创建它们。
我做了一个视图控制器来测试这个想法,最初 currentView 将从情节提要中实例化,然后以编程方式创建,每次按下按钮时都会创建一个新视图来替换另一种,执行动画并将新视图设置为下一次按下按钮时的currentView。
class ViewController: UIViewController {
@IBOutlet weak var currentView: UIView!
@IBOutlet weak var container: UIView!
var flipped = false
@IBAction func buttonPressed(sender: AnyObject) {
let new: UIView!
if flipped {
new = UIImageView(frame: container.bounds)
new.backgroundColor = UIColor.redColor()
flipped = false
}
else {
new = UIButton(frame: container.bounds)
new.backgroundColor = UIColor.blueColor()
flipped = true
}
let options: UIViewAnimationOptions = [.TransitionFlipFromLeft, .AllowUserInteraction, .BeginFromCurrentState]
UIView.transitionFromView(currentView, toView: new, duration: 0.5, options: options, completion: nil)
self.currentView = new
}
}
希望这对您有所帮助:)
您可以执行以下操作(我假设 share view
包含图像视图和按钮):
override func viewDidLoad()
{
super.viewDidLoad()
shareView = UIView(frame: CGRectMake(30, 100, 300, 400)) //set the frame of the holder view
flippedView = UIView(frame: shareView!.bounds) //setup flipped view
flippedView!.backgroundColor = UIColor.redColor() //for test
isFlipped = false //initially not flipped
//set up the initial view with image and button
aImageView = UIImageView(frame: shareView!.bounds)
aImageView!.image = UIImage(named: "1.jpg")
shareButton = UIButton(type: .System)
shareButton!.setTitle("share", forState: .Normal)
shareButton!.frame = CGRectMake(0, 0, 150, 50)
shareButton!.addTarget(self, action: "shareButtonAction", forControlEvents: .TouchUpInside)
//add both imageview and button to holder view
shareView!.addSubview(aImageView!)
shareView!.addSubview(shareButton!)
//finally add holder to self view
self.view.addSubview(shareView!)
}
这里,使用transitionWithView
方法是不能去掉image view的super view的。您能做的最好的事情就是用翻转后要显示的新视图替换图像视图。再一次,您可以通过将其添加为子视图来翻转回图像视图。例如:
func shareButtonAction()
{
if (self.isFlipped! == false)
{
UIView.transitionWithView(shareView!, duration: 0.5, options:.TransitionFlipFromRight, animations: { () -> Void in
// self.aImageView!.image = UIImage(named: "2.jpg")
//hear remove the imageview add new view, say flipped view
self.aImageView!.removeFromSuperview()
self.shareView!.addSubview(self.flippedView!)
}, completion: { (Bool) -> Void in
self.isFlipped! = true
self.shareView!.bringSubviewToFront(self.shareButton!) //button should be top of the holder view
})
}
else
{
UIView.transitionWithView(shareView!, duration: 0.5, options:.TransitionFlipFromRight, animations: { () -> Void in
//move back, remove flipped view and add the image view
self.flippedView!.removeFromSuperview()
self.shareView!.addSubview(self.aImageView!)
}, completion: { (Bool) -> Void in
self.isFlipped! = false
self.shareView!.bringSubviewToFront(self.shareButton!)
})
}
}