簡體   English   中英

如何使用Swift從右到左和從左到右制作自定義視圖的動畫

[英]How to animate a Custom View Right to Left and Left to Right using Swift

我設計了一個視圖。 通常,當我單擊一個按鈕時,此視圖將顯示和隱藏。 現在,我嘗試使用“從左到右”和“從右到左”為該視圖設置動畫。 以下是我的代碼,但無法正常工作。 請檢查一下。

if(self.openedView) {
        self.openedView = false;
        UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseIn, animations: {() -> Void in



        }, completion: {(_ finished: Bool) -> Void in
        })

         shareMainView.isHidden = true;
    } else {
         self.openedView = true;
        UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut, animations: {() -> Void in
        }, completion: {(_ finished: Bool) -> Void in
            /*done*/
        })

        shareMainView.isHidden = false;
    }

shareMainView.isHidden = false; 這是我的視圖,它將隨動畫一起打開和隱藏。

兄弟根據您發布的代碼,您既不會更改shareMainView的框架,也不會對其進行任何約束。 因此,請嘗試使用約束幀,例如在動畫塊中將打開狀態設置為一定寬度(例如150),將關閉狀態設置為0.0。

不要忘記在動畫代碼的最后使用shareMainView.layoutIfNeeded() ,並在完成塊中使用shareMainView.isHidden = false 正確顯示動畫會有所幫助。

鑒於確實加載:

shareMainView.frame = CGRectMake(_aview.frame.origin.x, _aview.frame.origin.y, 0, _aview.frame.size.height);

在按鈕上單擊:

if(self.openedView) {
        self.openedView = false;
        UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseIn, animations: {() -> Void in
        shareMainView.layoutIfNeeded()
        shareMainView.frame = CGRectMake(_aview.frame.origin.x, _aview.frame.origin.y, 200, _aview.frame.size.height);


        }, completion: {(_ finished: Bool) -> Void in
        })

         shareMainView.isHidden = true;
    } else {
         self.openedView = true;
        UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut, animations: {() -> Void in
        shareMainView.layoutIfNeeded()
        shareMainView.frame = CGRectMake(_aview.frame.origin.x, _aview.frame.origin.y, 0, _aview.frame.size.height);

        }, completion: {(_ finished: Bool) -> Void in
            /*done*/
        })

        shareMainView.isHidden = false;
    }

寬度可以與您要擴展的一樣多。例如,我有200個用戶,在視圖中加載時為0,然后在擴展時為200,在折疊時再次為0。 希望它會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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