プログラムでメニューを構築しましたが、各メニュー項目をストーリーボード内の別のviewControllerにリンクしたいので、UIを制御できます。これまでのところ、私は持っています:ViewControllerにメニューをリンクするにはどうすればいいですか?
import UIKit
class Setting: NSObject {
let name: String
let imageName: String
init(name: String, imageName: String) {
self.name = name
self.imageName = imageName
}
}
class SettingsLauncher: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@available(iOS 6.0, *)
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return settings.count
}
let blackView = UIView()
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}()
let cellId = "cellId"
let cellHeight: CGFloat = 40
let settings: [Setting] = {
return [Setting(name: "News", imageName: "news"), Setting(name: "Settings", imageName: "settings"), Setting(name: "UI list", imageName: "list"), Setting(name: "Book", imageName: "book"), Setting(name: "Me", imageName: "me"), Setting(name: "Cancel", imageName: "close")]
}()
func CGRectMake(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect {
return CGRect(x: x, y: y, width: width, height: height)
}
var TableViewController: TableViewController?
func showSettings() {
if let window = UIApplication.shared.keyWindow {
blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))
window.addSubview(blackView)
window.addSubview(collectionView)
let height: CGFloat = CGFloat(settings.count) * cellHeight
let y = window.frame.height - height
collectionView.frame = CGRectMake(0, window.frame.height, window.frame.width,height)
blackView.frame = window.frame
blackView.alpha = 0
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 1
self.collectionView.frame = self.CGRectMake(0, y, self.collectionView.frame.width, self.collectionView.frame.height)
}, completion: nil)
}
}
func handleDismiss() {
UIView.animate(withDuration: 0.5) {
self.blackView.alpha = 0
if let window = UIApplication.shared.keyWindow {
self.collectionView.frame = self.CGRectMake(0, window.frame.height, self.collectionView.frame.width, self.collectionView.frame.height)
}
}
}
//func numberOfSections(in collectionView: UICollectionView) -> Int {
// return 3
//}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SettingCell
let setting = settings[indexPath.item]
cell.setting = setting
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width,height: cellHeight)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//print(setting.name)
// handleDismiss()
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 0
if let window = UIApplication.shared.keyWindow {
self.collectionView.frame = self.CGRectMake(0, window.frame.height, self.collectionView.frame.width, self.collectionView.frame.height)
}
}) { (completed: Bool) in
let setting = self.settings[indexPath.item]
if setting.name != "Cancel" {
self.TableViewController?.showControllerForSetting(setting: setting)
}
}
}
override init() {
super.init()
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(SettingCell.self, forCellWithReuseIdentifier: cellId)
}
}
メニューリストは、コードから見ることができます配列から取得されます。私はそれぞれ独自のviewControllerにリンクしたいと思います。私はこれについてオンラインのチュートリアルに従いました。このメニューシステムはビューの一番下からスライドして自分自身を明らかにします。
に設定を渡す以下の方法
を追加あなたのUIスクリーンショットを追加してください。 –
メニューの意味は?またはどのUI?メニューの – Sole
の出力。 –