2016-07-26 21 views
2

如果我的故事板中有一個視圖設置,當按下按鈕時,有沒有一種方法可以使我的視圖(自定義寬度和高度)從屏幕底部向上滑動?我想讓屏幕重疊(這樣你仍然可以按下屏幕下方的東西)。讓View從底部滑入Swift中?

我該如何設置?

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

有沒有一種方法可以完成我在上面做的事情,除了我在故事板中創建的視圖?

+0

你嘗試過什麼?這可以通過多種方式完成,包括框架或自動佈局,UIView動畫等。 – paulvs

+0

沒有默認的過渡樣式會給你這種效果。 – David

+0

@paulvs看到我上面添加的代碼 - 我如何鏈接故事板視圖來替換我現在使用的'collectionView' –

回答

5

我已經爲你做了一個小小的演示:創建了新項目,並在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) 

    } 

} 
1

SwiftStudier的回答更新的斯威夫特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) 
    } 
}