[英]UILabel with Tap Gesture Recognizer not working
I have designed a UICollectionViewCell
using XIB
and in that custom cell I have an UILabel
whose user interaction I have enabled. 我使用
XIB
设计了一个UICollectionViewCell
,在该自定义单元中,我有一个UILabel
,我已启用了它的用户交互功能。
In my viewcontroller
when I am designing the cell
, Here is my code. 在设计
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];
}
}
But the added tap gesture does not work. 但是添加的轻击手势不起作用。
My suggestion is to rework your design and change the UILabel
to an UIButton
, because UIButton
simply handles tap recognition, and also you do not have any issue of tap forwarded also to the UICollectionView
( didSelectItemAtIndexPath:
will be called indeed). 我的建议是重新设计,并将
UILabel
更改为UIButton
,因为UIButton
只是处理拍子识别,并且您也没有将拍子问题转发到UICollectionView
( didSelectItemAtIndexPath:
的确会被调用)。
So, change the label with a button and set buyNowClicked:
method at TouchUpInside
event on it. 因此,使用按钮更改标签,并在其上的
TouchUpInside
事件上设置buyNowClicked:
方法。
UPDATE: If you can't remove the UILabel for any reason, then put a UIButton
under the UILabel
(with same frame
and eventually NSConstraints
) and move buyNowClicked:
method at TouchUpInside
event on the button and then you win easily 更新:如果由于某种原因无法删除UILabel,则将
UIButton
放在UILabel
(具有相同frame
并最终NSConstraints
),然后将TouchUpInside
事件上的buyNowClicked:
方法TouchUpInside
按钮上,然后轻松获胜
create custome cell with tag property and use 使用标签属性创建定制单元并使用
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld", (long)indexPath.row);
}
Note: added TapGestureRecognizer somewhere and it prevents selecton of cell didselectitematindexpath
注意:在某处添加了TapGestureRecognizer,它可以防止选择单元格的
didselectitematindexpath
You have to add this code into the Your Custom UICollectionViewCell
class. 您必须将此代码添加到“自定义
UICollectionViewCell
类中。
Then create a delegate which will respond to the tap. 然后创建一个代表,将响应点击。
Assign that delegate in cellForRowAtIndexPath
of Collection View. 在集合视图的
cellForRowAtIndexPath
中分配该委托。
let me know if you need detailed explanation. 如果您需要详细说明,请告诉我。
Edit 1: Code: 编辑1:代码:
MyCell.swift: 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: 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
}
}
Try this :- 尝试这个 :-
-(void)buyNowClicked:(UITapGestureRecognizer*)tap
{
NSLog(@"buyNowClicked here");
[self buyService:tap.view.tag];
}
Use this code for tap gesture: - 使用此代码进行点击手势:-
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.