快速下降

时间:2015-01-14 12:13:29

标签: ios swift uiviewanimation uiviewanimationtransition

全部,

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

我通过使用动画块完成了很多操作 - 从x = 30到x = 400,因此它会将框关闭。当成功块运行时,它将UIScollview从hidden = false中转换。因此它在完成的动画之后显示UIScoll视图。

这是最好的方法吗?我试图实现这些结果

enter image description here

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

这是过滤器未曝光之前的图像。 enter image description here

因此NAV栏下面有一个蓝点。

任何人都可以帮助我,或者这是背部练习吗?

2 个答案:

答案 0 :(得分: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                       
    })


}

答案 1 :(得分:1)

这个问题现在有点老了,但是如果有人也是这样的话。 我建议一定要看看这篇文章

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

Awesome-iOS git repo

enter image description here

我最喜欢的是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")
    }
}

以这种方式在View-controller中使用上面

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
    }
}