我正在尝试为我的游乐场创建一个集合视图类。我有这个代码。我在Xcode playground中创建一个UICollectionView后会在我的主文件中使用它。
这是我的代码:
import UIKit
import SceneKit
public class MenuCollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
public init(frame: CGRect) {
let layout = UICollectionViewFlowLayout()
super.init(frame: frame, collectionViewLayout: layout)
self.dataSource = self
self.delegate = self
self.backgroundColor = UIColor(red: 0, green: 0, blue: 38, alpha:1.0)
self.register(MenuCollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")
}
required public init(coder aDecoder: NSCoder) {
fatalError("This class does not support NSCoding")
}
public func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MenuCell", for: indexPath) as! MenuCollectionViewCell
cell.PlanetName.text = Constants.names[indexPath.row]
cell.PlanetImage.image = UIImage(named: Constants.imagesNames[indexPath.row])?.cropToSquare().cropToCircle()
return cell
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (self.frame.size.width/2)-10, height: (((self.frame.size.width/2)-10)*1.4)+25)
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! MenuCollectionViewCell
self.tag = indexPath.row
UIView.animate(
withDuration: 0.3,
delay: 0.0,
usingSpringWithDamping: 1.1,
initialSpringVelocity: 1.5,
options: UIViewAnimationOptions.curveEaseInOut,
animations: ({
cell.PlanetImage.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
}), completion: {finished in
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MenuItemSelected"), object: nil)
UIView.animate(
withDuration: 0.3,
delay: 0.0,
usingSpringWithDamping: 1.1,
initialSpringVelocity: 1.5,
options: UIViewAnimationOptions.curveEaseInOut,
animations: ({
cell.PlanetImage.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
}), completion: nil)
})
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 5, 0, 5)
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
}
当我使用下一行代码时出现该错误:
let menuView1 = MenuCollectionView(frame: CGRect.init(x: -(viewWidth/2.2), y: 0.0, width: viewWidth/2.2, height: viewHeight))
运行项目时出现的错误是:
EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)。
该过程一直处于被中断的位置,使用“thread return -x”返回到表达式求值之前的状态。
我的Xcode版本是9.2,我使用的是Swift 4.如果您有任何疑问,请发表评论,我会提供有关错误的更多信息。