【问题标题】:Having View Slide in From Bottom in Swift?在 Swift 中从底部滑入视图?
【发布时间】:2016-07-26 16:21:57
【问题描述】:

如果我在情节提要中有一个视图设置,是否可以在按下按钮时让该视图(具有自定义宽度和高度)从屏幕底部向上滑动?我希望屏幕只是覆盖(这样你仍然可以在下面的屏幕上按下东西)。

我该如何设置?

func isChecked(){

    let window = UIApplication.sharedApplication().keyWindow

    window!.addSubview(collectionView)
    let height: CGFloat = 250
    let y = window!.frame.height - 250
    collectionView.frame = CGRect(x: 0, y: window!.frame.height, width: window!.frame.width, height: height)

    UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .CurveEaseOut, animations: {
        self.collectionView.frame = CGRectMake(0, y, self.collectionView.frame.width, self.collectionView.frame.height)
        }, completion: nil)
}

func isUnchecked(){
    let window = UIApplication.sharedApplication().keyWindow
    UIView.animateWithDuration(0.3, animations: {
        self.collectionView.frame = CGRectMake(0, window!.frame.height, self.collectionView.frame.width, self.collectionView.frame.height)
    })
}

除了我在故事板中创建的视图之外,有没有一种方法可以完成我在上面所做的事情?

【问题讨论】:

  • 你试过什么?这可以通过多种方式完成,包括框架或自动布局、UIView 动画等。
  • 没有默认的过渡风格可以给你这种效果。
  • @paulvs 查看我在上面添加的代码 - 如何链接故事板视图来替换我现在使用的 collectionView
  • 最好的方法是创建一个UIView子类(例如MyView),创建一个同名的xib(例如'MyView.xib',这是通过@987654325完成的@)。然后使用let myView = UINib(nibName: "MyView", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! MyView 以编程方式实例化它。
  • 太棒了!它完美地工作!感谢@paulvs 的帮助

标签: ios swift xcode storyboard


【解决方案1】:

Swift 5 版本:

import UIKit

class ViewController: UIViewController {

   let collectionView: UICollectionView = {
   let frame = CGRect(x: 0, y: 50, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - 50)
   let col = UICollectionView(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
    col.layer.borderColor = UIColor.red.cgColor
    col.layer.borderWidth = 1.0
    col.backgroundColor = UIColor.yellow
    return col
  }()

        let switchView = UISwitch()

        func switched(s: UISwitch){
           let origin: CGFloat = s.on ? view.frame.height : 50
            UIView.animate(withDuration: 0.35) {
                self.collectionView.frame.origin.y = origin 
            }
        }

        override func viewDidLoad() {
            super.viewDidLoad()

            switchView.frame = CGRect(x: 0, y: 20, width: 40, height: 20)
            switchView.addTarget(self, action: #selector(switched), forControlEvents: .valueChanged)

            view.addSubview(switchView)
            view.addSubview(collectionView)

        }

    }

我为你做了一个小演示:创建新项目并在 ViewController.swift 中编写代码,仅此而已。觉得有帮助

import UIKit

class ViewController: UIViewController {

    let collectionView: UICollectionView = {
        let frame = CGRect(x: 0, y: 50, width: UIScreen.mainScreen().bounds.size.width, height: UIScreen.mainScreen().bounds.size.height - 50)
        let col = UICollectionView(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        col.layer.borderColor = UIColor.redColor().CGColor
        col.layer.borderWidth = 1.0
        col.backgroundColor = UIColor.yellowColor()
        return col
    }()

    let switchView = UISwitch()

    func switched(s: UISwitch){
        let origin: CGFloat = s.on ? view.frame.height : 50
        UIView.animateWithDuration(0.35) { 
            self.collectionView.frame.origin.y = origin
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        switchView.frame = CGRect(x: 0, y: 20, width: 40, height: 20)
        switchView.addTarget(self, action: #selector(switched), forControlEvents: .ValueChanged)

        view.addSubview(switchView)
        view.addSubview(collectionView)

    }

}

【讨论】:

    【解决方案2】:

    SwiftStudier 为 Swift 3 更新了答案:

    import UIKit
    
    class ViewController: UIViewController {
    
        let collectionView: UICollectionView = {
            let frame = CGRect(x: 0, y: 50, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - 50)
            let col = UICollectionView(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
            col.layer.borderColor = UIColor.red.cgColor
            col.layer.borderWidth = 1.0
            col.backgroundColor = UIColor.yellow
            return col
        }()
    
        let switchView = UISwitch()
    
        func switched(s: UISwitch){
            let origin: CGFloat = s.isOn ? view.frame.height : 50
            UIView.animate(withDuration: 0.35) {
                self.collectionView.frame.origin.y = origin
            }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            switchView.frame = CGRect(x: 0, y: 20, width: 40, height: 20)
            switchView.addTarget(self, action: #selector(switched), for: .valueChanged)
    
            view.addSubview(switchView)
            view.addSubview(collectionView)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多