[英]UILabel with Tap Gesture Recognizer not working
我使用XIB
設計了一個UICollectionViewCell
,在該自定義單元中,我有一個UILabel
,我已啟用了它的用戶交互功能。
在設計cell
,在viewcontroller
,這是我的代碼。
UITapGestureRecognizer *buyNowTapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buyNowClicked:)];
buyNowTapped.numberOfTapsRequired = 1.0;
cell.buy.tag = indexPath.row;
cell.buy.userInteractionEnabled = YES;
[cell.buy addGestureRecognizer:buyNowTapped];
-(void)buyNowClicked : (id)sender
{
UIButton *button;
UILabel *label;
if([sender isKindOfClass:[UIButton class]])
{
button = (UIButton *)sender;
[self buyService:button.tag];
}
else if ([sender isKindOfClass:[UILabel class]])
{
label = (UILabel *)sender;
[self buyService:label.tag];
}
}
但是添加的輕擊手勢不起作用。
我的建議是重新設計,並將UILabel
更改為UIButton
,因為UIButton
只是處理拍子識別,並且您也沒有將拍子問題轉發到UICollectionView
( didSelectItemAtIndexPath:
的確會被調用)。
因此,使用按鈕更改標簽,並在其上的TouchUpInside
事件上設置buyNowClicked:
方法。
更新:如果由於某種原因無法刪除UILabel,則將UIButton
放在UILabel
(具有相同frame
並最終NSConstraints
),然后將TouchUpInside
事件上的buyNowClicked:
方法TouchUpInside
按鈕上,然后輕松獲勝
使用標簽屬性創建定制單元並使用
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld", (long)indexPath.row);
}
注意:在某處添加了TapGestureRecognizer,它可以防止選擇單元格的didselectitematindexpath
您必須將此代碼添加到“自定義UICollectionViewCell
類中。
然后創建一個代表,將響應點擊。
在集合視圖的cellForRowAtIndexPath
中分配該委托。
如果您需要詳細說明,請告訴我。
編輯1:代碼:
MyCell.swift:
import UIKit
protocol MyCellDelegate {
func lblNameTapped(cell: MyCell)
}
class MyCell: UICollectionViewCell {
@IBOutlet var lblName: UILabel!
var lblNameTapRec:UITapGestureRecognizer!
var delegate: MyCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
lblNameTapRec = UITapGestureRecognizer(target: self, action: #selector(MyCell.lblNameTapped(_:)))
lblName.userInteractionEnabled = true
lblName.addGestureRecognizer(lblNameTapRec)
}
func lblNameTapped(sender: AnyObject){
delegate?.lblNameTapped(self)
}
}
ViewController.Swift:
import UIKit
class WallOfPraizeVC: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,MyCellDelegate {
@IBOutlet var collectionView: UICollectionView!
//DelegateMethod
func lblNameTapped(cell: MyCell) {
let indexPath = self.collectionView.indexPathForCell(cell)
print(indexPath!.row)
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! MyCell
cell.delegate = self
return cell
}
}
嘗試這個 :-
-(void)buyNowClicked:(UITapGestureRecognizer*)tap
{
NSLog(@"buyNowClicked here");
[self buyService:tap.view.tag];
}
使用此代碼進行點擊手勢:-
UITapGestureRecognizer *buyNowTapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buyNowClicked:)];
buyNowTapped.numberOfTapsRequired = 1.0;
cell.buy.tag = indexPath.row;
cell.buy.userInteractionEnabled = YES;
[cell.buy addGestureRecognizer:buyNowTapped];
Try this :
-(void)buyNowClicked : :(UITapGestureRecognizer*)recognizer
{
UIView *view = [recognizer view];
NSLog(@"tag %ld",(long)view.tag);
UIButton *button;
UILabel *label;
if([view isKindOfClass:[UIButton class]])
{
button = (UIButton *)sender;
[self buyService:button.tag];
}
else if ([view isKindOfClass:[UILabel class]])
{
label = (UILabel *)sender;
[self buyService:label.tag];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.