저는 신속하게 프로젝트를하고 있습니다. 콜렉션 뷰를 정의하고 셀을 사용자 정의하여 이미지가 아래처럼 보이게 만듭니다. 자, 일단 셀을 클릭하면, 제 기능은 어떤 셀을 클릭했는지를 알려주지 만 더 자세한 정보를 얻고 싶습니다. 예를 들어 셀 번호 2의 그림을 얻습니다. 그 목적을 위해 탭 제스처를 정의하려고 시도했지만 작동하지 않는 것 같습니다. 여기에 내 모든 코드 :UICollectionView에서 사용자 정의 된 셀의 어떤 부분을 클릭했는지 알아 보는 방법은 무엇입니까?
수입 UIKit 수입 AVFoundation 수입 AVKit
class ViewUSRController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource {
@IBOutlet var collectionView: UICollectionView!
var cell : MyCollectionViewCell?
var names = ["Danial" , "dkfloza" , "Kosarifar" , "IOS" , "Learning", "sina" ]
var likes = ["23" , "7" , "17" , "100K" , "1000K" , "100"]
var dislikes = ["0","0","0","0","0","0"]
var playerViewController = AVPlayerViewController()
var playerView = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
self.collectionView.delegate = self
let tap = UITapGestureRecognizer(target: self.cell?.myLabel, action:#selector(handleTap))
tap.numberOfTapsRequired = 1
cell?.myLabel.addGestureRecognizer(tap)
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return self.names.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "id", for: indexPath as IndexPath) as! MyCollectionViewCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
cell?.myLabel?.text = self.names[indexPath.row]
cell?.myLabel?.textColor = .black
cell?.likes_num?.text = likes[indexPath.row]
cell?.dislike_num?.text = dislikes[indexPath.row]
cell?.likes_num?.textColor = .black
cell?.dislike_num?.textColor = .black
return cell!
}
//
//
// func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// // handle tap events
// print("You selected cell #\(indexPath.item)!")
//
//
//
//
// }
func handleTap(){
print("hello")
}
}
class MyCollectionViewCell : UICollectionViewCell{
//appClub
@IBOutlet var myLabel: UILabel!
//imagePlace
@IBOutlet var viewTesting: UIView!
//likes
@IBOutlet var likes_num: UILabel!
//dislikes
@IBOutlet var dislike_num: UILabel!
}
레이블에 단추를 넣고 가운데와 같은 너비 + 높이의 단추를 자동 레이아웃 한 다음 대리자 또는 클로저 기반 동작을 만들 수 있습니다. – SeanLintern88
여기에는 너무 많은 우수 사례가 있습니다. 그러나 각각의 연습에서 자신 만의 'UITouch'객체를 처리해야합니다. 'touches' 이벤트와 콜렉션 뷰 델리게이트를 함께 사용해보십시오. 터치 된 항목 (셀)과 콜렉션 뷰에서의 터치 위치 (또는 셀까지)를 식별하십시오. 나중에 해당 터치 위치를 터치 된 항목의 좌표계에 매핑하여 뷰를 가져올 수 있습니다. –
작업을 수행하기 위해 제스처를 사용하려고하면, 응답자 ('UIResponder' 개체) 계층을 변경할 수 있습니다. 이것은 당신을 위해 상황을 악화시킬 것입니다. –