过渡时的致命错误
Fatal Error while transition
Error : "fatal error: unexpectedly found nil while unwrapping an Optional value" during transition between two view on same view controller
当我调用 Flip1
函数时,出现上述错误。
代码如下:
@IBAction func Flip(_ sender: AnyObject){
UIView.transition(from: Back, to: Front, duration: 0.5, options: UIViewAnimationOptions.transitionFlipFromLeft, completion: nil)
}
@IBAction func Flip1(_ sender: AnyObject){
UIView.transition(from: Front, to: Back, duration: 1, options: UIViewAnimationOptions.transitionFlipFromRight, completion: nil)
}
这是您问题的答案:transitionFromView:toView:duration:options:completion:
Parameters (According to Documentation):
fromView
The starting view for the transition. By default, this view is removed from its superview as part of the transition.
toView
The ending view for the transition. By default, this view is added to the superview of fromView as part of the transition.
您问题的解决方法:
如果您希望两个视图都在内存中,请使用 transitionWithView:duration:options:animations:completion: 而不是 transitionFromView:toView:duration:options:completion:。
试试这个:
添加包含 'Front' 和 'Back' 视图的 UIView。 (添加一个新视图作为 'Front' 和 'Back' 视图的超级视图)
对 'super view' 执行翻转动画以及对 'Front' 和 'Back' 视图执行隐藏和取消隐藏操作。使用 transitionWithView:duration:options:animations:completion: 动画块
示例代码:
@IBOutlet var superView: UIView!
@IBOutlet var Back: UIView!
@IBOutlet var Front: UIView!
@IBAction func Flip(_ sender: AnyObject){
self.Front.isHidden = false
self.Front.alpha = 0.1
UIView.transition(with: superView, duration: 0.5, options: .transitionFlipFromLeft, animations: {
self.Back.alpha = 0.1
self.Front.alpha = 1.0
}) { (isCompleted) in
self.Back.isHidden = true
}
}
@IBAction func Flip1(_ sender: AnyObject){
self.Back.isHidden = false
self.Back.alpha = 0.1
UIView.transition(with: superView, duration: 0.5, options: .transitionFlipFromRight, animations: {
self.Front.alpha = 0.1
self.Back.alpha = 1.0
}) { (isCompleted) in
self.Front.isHidden = true
}
}
是啊,我终于找到错误了。
只需将插座设为强参考或弱参考即可:
@IBOutlet var Front: UIView!
@IBOutlet var Back: UIView!
大功告成!
Error : "fatal error: unexpectedly found nil while unwrapping an Optional value" during transition between two view on same view controller
当我调用 Flip1
函数时,出现上述错误。
代码如下:
@IBAction func Flip(_ sender: AnyObject){
UIView.transition(from: Back, to: Front, duration: 0.5, options: UIViewAnimationOptions.transitionFlipFromLeft, completion: nil)
}
@IBAction func Flip1(_ sender: AnyObject){
UIView.transition(from: Front, to: Back, duration: 1, options: UIViewAnimationOptions.transitionFlipFromRight, completion: nil)
}
这是您问题的答案:transitionFromView:toView:duration:options:completion:
Parameters (According to Documentation):
fromView
The starting view for the transition. By default, this view is removed from its superview as part of the transition.toView
The ending view for the transition. By default, this view is added to the superview of fromView as part of the transition.
您问题的解决方法:
如果您希望两个视图都在内存中,请使用 transitionWithView:duration:options:animations:completion: 而不是 transitionFromView:toView:duration:options:completion:。
试试这个:
添加包含 'Front' 和 'Back' 视图的 UIView。 (添加一个新视图作为 'Front' 和 'Back' 视图的超级视图)
对 'super view' 执行翻转动画以及对 'Front' 和 'Back' 视图执行隐藏和取消隐藏操作。使用 transitionWithView:duration:options:animations:completion: 动画块
示例代码:
@IBOutlet var superView: UIView!
@IBOutlet var Back: UIView!
@IBOutlet var Front: UIView!
@IBAction func Flip(_ sender: AnyObject){
self.Front.isHidden = false
self.Front.alpha = 0.1
UIView.transition(with: superView, duration: 0.5, options: .transitionFlipFromLeft, animations: {
self.Back.alpha = 0.1
self.Front.alpha = 1.0
}) { (isCompleted) in
self.Back.isHidden = true
}
}
@IBAction func Flip1(_ sender: AnyObject){
self.Back.isHidden = false
self.Back.alpha = 0.1
UIView.transition(with: superView, duration: 0.5, options: .transitionFlipFromRight, animations: {
self.Front.alpha = 0.1
self.Back.alpha = 1.0
}) { (isCompleted) in
self.Front.isHidden = true
}
}
是啊,我终于找到错误了。
只需将插座设为强参考或弱参考即可:
@IBOutlet var Front: UIView!
@IBOutlet var Back: UIView!
大功告成!