Ios 动画在Swift中显示和隐藏视图

Ios 动画在Swift中显示和隐藏视图,ios,swift,animation,uiview,Ios,Swift,Animation,Uiview,我有这个功能: func showwAndHideFilterMenu(category : Int) { if showFilterMenu == false{ UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseInOut, animations: { self.filterView.isHidden = false self.showFilte

我有这个功能:

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
        }
    }
}
我有一个显示和隐藏视图的函数。我想添加动画以显示/隐藏此视图。怎么做?动画的方向将是从上到下


有人知道怎么做吗?

对于UIView淡入淡出的动画,需要操纵alpha属性而不是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

            }
        }
    }

操纵alpha值,并在完成块中使用isHidden属性。对于自上而下的动画,请使用@iOS Geek的答案