gpt4 book ai didi

Swift 4 - 从类 'NSObject' 和 'UICollectionViewFlowLayout' 多重继承

转载 作者:可可西里 更新时间:2023-10-31 23:57:32 24 4
gpt4 key购买 nike

我正在尝试以编程方式将集合单元格的大小设置为框架的宽度,但是,当我运行该应用程序时,单元格大小不会改变。这是调用 Swift 4 和 Xcode 9 的正确函数吗?

import UIKit

class SettingsLauncher: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewFlowLayout {
let blackView = UIView()

let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}()

let cellID = "cell"

@objc func showMenu() {
// Show menu

if let window = UIApplication.shared.keyWindow { // Get the size of the entire window

blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)

let height: CGFloat = 200
let y = window.frame.height - height // The y value to appear at the bottom of the screen
collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: height)

// Add gesture recognizer on black view to dismiss menu

blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))

window.addSubview(blackView)
window.addSubview(collectionView)

blackView.frame = window.frame
blackView.alpha = 0

// Slow the animation down towards the end (curveEasOut)
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
// Animate black view
self.blackView.alpha = 1

// Animate collection view
self.collectionView.frame = CGRect(x: 0, y: y, width: self.collectionView.frame.width, height: height)
}, completion: nil)
}
}

@objc func handleDismiss() {
// Dimisses menu view

UIView.animate(withDuration: 0.5) {
self.blackView.alpha = 0

if let window = UIApplication.shared.keyWindow {

self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
}
}
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath)
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 50)
}

override init() {
super.init()

collectionView.delegate = self
collectionView.dataSource = self

collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellID)

}
}

编辑:

这个 TableView 从页面底部开始动画,并呈现一个 Collection View ,它是从使用弹出菜单的 View Controller 调用的。

我现在得到错误:

Multiple inheritance from classes 'NSObject' and 'UICollectionViewFlowLayout'

最佳答案

UICollectionViewFlowLayout 不是一个协议(protocol),它是一个类。您需要使用 UICollectionViewDelegateFlowLayout 而不是 UICollectionViewFlowLayout

改变

class SettingsLauncher: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewFlowLayout

class SettingsLauncher: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout

关于Swift 4 - 从类 'NSObject' 和 'UICollectionViewFlowLayout' 多重继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46451458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com