[英]Having trouble with UIView slide animation from left to right?
我试图动画2个UIViews
,UIView1存在,并在一个按钮水龙头,UIView2由右至左滑动并占据屏幕,移动UIView1闪开。
那部分我可以使用slideLeftTransitionFromView
成功实现。 但是,再次单击按钮以使用slideRightTransitionFromView从左向右滑动slideRightTransitionFromView
,UIView1在动画过程中会以某种方式消失。 UIView2确实可以成功地从左向右滑动,但是我看不到UIView1。
我敢肯定, slideRightTransitionFromView
遵循类似的逻辑作为slideLeftTransitionFromView
,但我无法得到它的工作。
这是两个功能:
func slideLeftTransitionFromView(view: UIView, toView: UIView, duration: NSTimeInterval, completion: ((Bool) -> Void)?)
{
if let container = view.superview
{
// Final position of outgoing view
let outgoingViewEndX = view.frame.origin.x - view.frame.size.width
// Final position of incoming view (existing frame)
let incomingViewEndX = view.frame.origin.x
// Start point of incoming view -- at the right edge of the outgoing view
let incomingViewStartX = view.frame.origin.x + view.frame.size.width
// Distance traveled by outgoing view
let outgoingDisplacement = view.frame.origin.x - outgoingViewEndX
toView.frame.origin = CGPointMake(incomingViewStartX, toView.frame.origin.y)
container.addSubview(toView)
dispatch_async(dispatch_get_main_queue(), { ()
UIView.animateWithDuration(duration, delay: 0, options: .CurveLinear, animations: {
view.frame.origin.x = outgoingViewEndX
toView.frame.origin.x = incomingViewEndX
}, completion: {(complete: Bool) in
// If the outgoing view is still in onscreen, keep sliding until it's offscreen
if outgoingViewEndX + view.frame.size.width > 0 {
// Adjust the duration for the final animation based on the distance it has to travel in order to keep the same velocity.
let finalDuration = duration * Double((outgoingViewEndX + view.frame.size.width)/outgoingDisplacement)
UIView.animateWithDuration(finalDuration, delay: 0, options: .CurveLinear, animations: {
view.frame.origin.x = -view.frame.size.width
}, completion: { (complete: Bool) in
completion?(complete)
view.removeFromSuperview()
})
}
else
{
completion?(complete)
view.removeFromSuperview()
}
})
})
}
}
func slideRightTransitionFromView(view: UIView, toView: UIView, duration: NSTimeInterval, completion: ((Bool) -> Void)?)
{
if let container = view.superview
{
// Final position of outgoing view
let outgoingViewEndX = view.frame.origin.x + view.frame.size.width
// Final position of incoming view (existing frame)
let incomingViewEndX = view.frame.origin.x
// Start point of incoming view -- at the left edge of the outgoing view
let incomingViewStartX = view.frame.origin.x - view.frame.size.width
// Distance traveled by outgoing view
let outgoingDisplacement = view.frame.origin.x + outgoingViewEndX
toView.frame.origin = CGPointMake(incomingViewStartX, toView.frame.origin.y)
container.addSubview(toView)
dispatch_async(dispatch_get_main_queue(), { ()
UIView.animateWithDuration(duration, delay: 0, options: .CurveLinear, animations: {
view.frame.origin.x = outgoingViewEndX
toView.frame.origin.x = incomingViewEndX
}, completion: {(complete: Bool) in
// If the outgoing view is still in onscreen, keep sliding until it's offscreen
if outgoingViewEndX + view.frame.size.width < 0 {
// Adjust the duration for the final animation based on the distance it has to travel in order to keep the same velocity.
let finalDuration = duration * Double((outgoingViewEndX + view.frame.size.width)/outgoingDisplacement)
UIView.animateWithDuration(finalDuration, delay: 0, options: .CurveLinear, animations: {
view.frame.origin.x = view.frame.size.width
}, completion: { (complete: Bool) in
completion?(complete)
view.removeFromSuperview()
})
}
else
{
completion?(complete)
view.removeFromSuperview()
}
})
})
}
}
这可能很简单,但我看不到。 似乎是什么问题?
谢谢
您可能已将
UIView IBOutlet
声明为弱变量
在slideLeftTransitionFromView
调用view.removeFromSuperview()
从视图层次结构中删除该视图,并且可以确定您的视图已被释放,因为它是一个弱var,不再是布局的一部分。
因此,稍后尝试再次将其添加为子视图时,您看不到它。 尝试使var成为强参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.