【问题标题】:Drop down in swift迅速下降
【发布时间】:2015-03-12 14:58:00
【问题描述】:

全部,

我想创建一个存储 UIScrollViews 等的下拉列表

我通过使用动画块做了很多这样的事情 - 从 x = 30 到 x = 400 ,所以它把盒子放下了。当成功块运行时,它会将 UIScollview 从 hidden = false .. 所以它在完成动画后显示 UIScoll 视图。

这是最好的方法吗?我正在努力实现这些结果

这是暴露的过滤器。所以动画运行以降低蓝色,然后我取消隐藏滚动视图。

这是之前的图像,当滤镜未曝光时。

因此在 NAV 栏下方有一个蓝色位。

谁能帮帮我,或者这是后面的练习?

【问题讨论】:

    标签: ios swift uiviewanimation uiviewanimationtransition


    【解决方案1】:

    如果您的目标是 iOS 7 或更高版本,您可以通过在下拉视图中放置两个具有不同优先级的高度约束来实现这一点,例如优先级 750 和显示状态的下拉高度常数以及 700 优先级和常数 0用于隐藏状态。接下来为了显示/隐藏下拉菜单,只需调用以下函数:

    func showView(dropDownView: UIView) {
    
        let constrains = (dropDownView.superview?.constraints() as [NSLayoutConstraint]) +
                (dropDownView.constraints() as [NSLayoutConstraint])
        for constrain  in constrains{
    
            if(constrain.priority == 650{
    
                constrain.priority = 750
    
           }
    
        }
    
        dropDownView.hidden = false
        UIView.animateWithDuration(0.4, animations: {
            dropDownView.alpha = 1
            dropDownView.superview?.layoutIfNeeded()
            }, completion: {
            (value: Bool) in
    
        })
    
    
    }
    
    func hideView(dropDownView: UIView) {
    
        let constrains = (dropDownView.superview?.constraints() as [NSLayoutConstraint]) +
                (dropDownView.constraints() as [NSLayoutConstraint])
        for constrain  in constrains{
    
            if(constrain.priority == 750{
    
                constrain.priority = 650
    
           }
    
        }
    
        UIView.animateWithDuration(0.4, animations: {
            dropDownView.alpha = 0
            dropDownView.superview?.layoutIfNeeded()
            }, completion: {
            (value: Bool) in
            dropDownView.hidden = true                       
        })
    
    
    }
    

    【讨论】:

    • 嗨 Zelb,我正在使用 ios7+,我想我应该看看 NSLayoutConstrait,因为我不太确定它是什么。
    【解决方案2】:

    这个问题现在有点老了,但是如果有人来同样的话。 我建议必须看看这篇文章

    Menu interface: Controller Libraries 感谢原作者Manoolia ;)

    Awesome-iOSgit repo

    我最喜欢的一个是Yalantis/GuillotineMenu

    示例代码

    只需创建 MenuViewController

       class MenuViewController: UIViewController, GuillotineMenu {
        //GuillotineMenu protocol
        var dismissButton: UIButton!
        var titleLabel: UILabel!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            dismissButton = UIButton(frame: CGRectZero)
            dismissButton.setImage(UIImage(named: "ic_menu"), forState: .Normal)
            dismissButton.addTarget(self, action: "dismissButtonTapped:", forControlEvents: .TouchUpInside)
    
            titleLabel = UILabel()
            titleLabel.numberOfLines = 1;
            titleLabel.text = "Activity"
            titleLabel.font = UIFont.boldSystemFontOfSize(17)
            titleLabel.textColor = UIColor.whiteColor()
            titleLabel.sizeToFit()
        }
    
        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            print("Menu: viewWillAppear")
        }
    
        override func viewDidAppear(animated: Bool) {
            super.viewDidAppear(animated)
            print("Menu: viewDidAppear")
        }
    
        override func viewWillDisappear(animated: Bool) {
            super.viewWillDisappear(animated)
            print("Menu: viewWillDisappear")
        }
    
        override func viewDidDisappear(animated: Bool) {
            super.viewDidDisappear(animated)
            print("Menu: viewDidDisappear")
        }
    
        func dismissButtonTapped(sende: UIButton) {
            self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
        }
    
        @IBAction func menuButtonTapped(sender: UIButton) {
            self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
        }
    
        @IBAction func closeMenu(sender: UIButton) {
            self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
        }
    
    }
    
    extension MenuViewController: GuillotineAnimationDelegate {
        func animatorDidFinishPresentation(animator: GuillotineTransitionAnimation) {
            print("menuDidFinishPresentation")
        }
        func animatorDidFinishDismissal(animator: GuillotineTransitionAnimation) {
            print("menuDidFinishDismissal")
        }
    
        func animatorWillStartPresentation(animator: GuillotineTransitionAnimation) {
            print("willStartPresentation")
        }
    
        func animatorWillStartDismissal(animator: GuillotineTransitionAnimation) {
            print("willStartDismissal")
        }
    }
    

    并以这种方式在您的视图控制器中使用上面

    class ViewController: UIViewController {
    
        let reuseIdentifier = "ContentCell"
        private let cellHeight: CGFloat = 210
        private let cellSpacing: CGFloat = 20
        private lazy var presentationAnimator = GuillotineTransitionAnimation()
    
        @IBOutlet var barButton: UIButton!
    
        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            print("VC: viewWillAppear")
        }
    
        override func viewDidAppear(animated: Bool) {
            super.viewDidAppear(animated)
            print("VC: viewDidAppear")
        }
    
        override func viewWillDisappear(animated: Bool) {
            super.viewWillDisappear(animated)
            print("VC: viewWillDisappear")
        }
    
        override func viewDidDisappear(animated: Bool) {
            super.viewDidDisappear(animated)
            print("VC: viewDidDisappear")
        }
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            let navBar = self.navigationController!.navigationBar
            navBar.barTintColor = UIColor(red: 65.0 / 255.0, green: 62.0 / 255.0, blue: 79.0 / 255.0, alpha: 1)
            navBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
        }
    
        @IBAction func showMenuAction(sender: UIButton) {
            let menuVC = storyboard!.instantiateViewControllerWithIdentifier("MenuViewController")
            menuVC.modalPresentationStyle = .Custom
            menuVC.transitioningDelegate = self
            if menuVC is GuillotineAnimationDelegate {
                presentationAnimator.animationDelegate = menuVC as? GuillotineAnimationDelegate
            }
            presentationAnimator.supportView = self.navigationController?.navigationBar
            presentationAnimator.presentButton = sender
            presentationAnimator.duration = 0.6
            self.presentViewController(menuVC, animated: true, completion: nil)
        }
    }
    
    // The following is just for the presentation. You can ignore it
    extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    
        func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 5
        }
    
        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            let cell: AnyObject? = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
            return cell as! UICollectionViewCell!
        }
    
        func collectionView(collectionView: UICollectionView,
                            layout collectionViewLayout: UICollectionViewLayout,
                            sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
            return CGSizeMake(CGRectGetWidth(collectionView.bounds) - cellSpacing, cellHeight)
        }
    }
    
    extension ViewController: UIViewControllerTransitioningDelegate {
    
        func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
            presentationAnimator.mode = .Presentation
            return presentationAnimator
        }
    
        func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
            presentationAnimator.mode = .Dismissal
            return presentationAnimator
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-12
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 2021-09-28
      相关资源
      最近更新 更多