ios - 在 Swift 中从底部滑入 View ?

标签 ios swift xcode storyboard

如果我的 Storyboard中有一个 View 设置,有没有一种方法可以让该 View (具有自定义宽度和高度)在按下按钮时从屏幕底部向上滑动?我希望屏幕只是覆盖(这样您仍然可以按下面屏幕上的东西)。

我该如何设置?

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

除了我在 Storyboard 中创建的 View 外,是否有一种方法可以完成我上面所做的事情?

最佳答案

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)

    }

}

关于ios - 在 Swift 中从底部滑入 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38595152/

相关文章:

ios - 在静态行 UITableView 中创建自定义单元格

ios - 导航栏中的按钮图像具有不同的外观。为什么?

objective-c - 自定义 UITableViewCell 中的属性未针对 UISearchDisplayController 的表进行初始化

ios - 接口(interface) 'my_framework' 上类别 'NSObject' 的重复定义

ios - 如何使用 SwiftUI 显示警报

xcode - NSTimer timeInterval 参数不允许变量

ios - AVFoundation 声音在 iOS 6 模拟器上工作但不是设备?

ios - 使用 NSTimer Swift 生成 SkShapeNodes

ios - 从 Obj-c 到 swift 的 Segue 找不到属性

iphone - 有没有办法从 iPhone 上的标准输入读取,无论是硬件还是模拟器?