簡體   English   中英

UIView幻燈片動畫從左到右有問題嗎?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM