我正在尝试动画2 UIViews
,UIView1存在,按下按钮,UIView2从右向左滑动并占据屏幕,将UIView1移开。
我可以使用slideLeftTransitionFromView
成功实现这一部分。但是,再次按下按钮可以用slideRightTransitionFromView
从左到右滑动UIView1,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()
}
})
})
}
}
这可能很简单,但我看不到它。什么似乎是问题?
由于
答案 0 :(得分:1)
您可能已将
UIView IBOutlet
声明为弱变量
slideLeftTransitionFromView
中的调用view.removeFromSuperview()
会从视图heirarchy中移除视图,并且我非常确定您的视图已被解除分配,因为它是一个弱变量而不再是布局的一部分。
因此,当您尝试将其再次添加为子视图时,您不会看到它 稍后的。尝试使用var强引用。