簡體   English   中英

Swift 中的動畫顯示和隱藏視圖

[英]Animation show and hide view in Swift

我有這個功能:

func showwAndHideFilterMenu(category : Int) {

    if showFilterMenu == false{
        UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseInOut, animations: {
            self.filterView.isHidden = false
            self.showFilterMenu = true       
        }) { (isCompleted) in
        }

    } else {
        UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseInOut, animations: {
            self.filterView.isHidden = true
            self.self.showFilterMenu = false       
        }) { (isCompleted) in
        }
    }
}

我有一個顯示和隱藏視圖的功能。 我想添加一個動畫來顯示/隱藏此視圖。 怎么做? 動畫的方向是從上到下。

有誰知道怎么做?

您需要操作 alpha 屬性而不是 UIView 淡入淡出動畫的 isHidden 屬性。

請嘗試以下操作:

func showAndHideFilterMenu(category : Int) {
    if showFilterMenu == false {
        self.filterView.alpha = 0.0
        self.filterView.isHidden = false
        self.showFilterMenu = true

        UIView.animate(withDuration: 0.6, 
                       animations: { [weak self] in
                        self?.filterView.alpha = 1.0
        })
    } else {
        UIView.animate(withDuration: 0.6,
                       animations: { [weak self] in
                        self?.filterView.alpha = 0.0
        }) { [weak self] _ in
            self?.filterView.isHidden = true
            self?.showFilterMenu = false
        }
    }
}

試試這將使您的視圖從上到下滑動,然后隱藏該視圖

//MARK: Slide View - Top To Bottom
func viewSlideInFromTopToBottom(view: UIView) -> Void {
    let transition:CATransition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    transition.type = CATransitionType.push
    transition.subtype = CATransitionSubtype.fromBottom
    view.layer.add(transition, forKey: kCATransition)
}

用法

// Cancel Button
@IBAction func cancelButtonAction(_ sender: Any) {            
   self.viewSlideInFromTopToBottom(view: hideAndShowPickerView)
   hideAndShowPickerView.isHidden = true
}

確保設置filterView.clipsToBounds = true

func showwAndHideFilterMenu(category : Int) {

        if showFilterMenu == false {

            var filterFrame = filterView.frame
            let actualHeight = filterFrame.size.height

            //initially set height to zero and in animation block we need to set its actual height.
            filterFrame.size.height = 0
            filterView.frame = frame

            UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseInOut, animations: {

                self.filterView.isHidden = false
                self.showFilterMenu = true

                //setting the actual height with animation
                filterFrame.size.height = actualHeight
                filterView.frame = filterFrame

            }) { (isCompleted) in

            }

        } else {

            var filterFrame = filterView.frame

            UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseInOut, animations: {

                self.filterView.isHidden = true
                self.showFilterMenu = false

                //set the height of the filter view to 0
                filterView.frame = filterFrame
                filterFrame.size.height = 0
                filterView.frame = frame

            }) { (isCompleted) in

            }
        }
    }

暫無
暫無

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

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