执行此操作时出现运行时错误。我需要帮助!我尝试删除一些代码,但我需要Touch手势,我认为选择器中存在错误,并且不知道如何解码。
让doubletapgesture:UITapGestureRecognizer = UITapGestureRecognizer(target:self,action:Selector((" processDoubleTap:")))我相信这一行导致错误
import UIKit
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
fileprivate let sectionInsets = UIEdgeInsets(top: 50.0, left: 10.0, bottom: 20.0, right: 10.0)
fileprivate let itemsPerRow: CGFloat = 9
var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
//var collectionView: UICollectionView!
// Do any additional setup after loading the view, typically from a nib.
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.white
self.view.addSubview(collectionView)
let doubletapgesture : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("processDoubleTap:")))
doubletapgesture.numberOfTapsRequired = 1
collectionView.addGestureRecognizer(doubletapgesture)
}
func processDoubleTap (sender: UITapGestureRecognizer)
{
if sender.state == UIGestureRecognizerState.ended
{
let point:CGPoint = sender.location(in: collectionView)
let indexPath = collectionView.indexPathForItem(at: point)
if indexPath != nil{
print("\(indexPath) clicked")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
//2
let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
let availableWidth = view.frame.width - paddingSpace
let widthPerItem = availableWidth / itemsPerRow
return CGSize(width: widthPerItem, height: widthPerItem)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return sectionInsets
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return sectionInsets.left
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int{
return 81
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.orange
return cell
}
}