我是手势识别器的新手所以也许这个问题听起来很愚蠢:我正在为一堆UIViews分配轻拍手势识别器.在该方法中,有可能找出以某种方式点击它们中的哪一个,或者我是否需要使用点击屏幕上的点找到它?
for (NSUInteger i=0; i<42; i++) { float xMultiplier=(i)%6; float yMultiplier= (i)/6; float xPos=xMultiplier*imageWidth; float yPos=1+UA_TOP_WHITE+UA_TOP_BAR_HEIGHT+yMultiplier*imageHeight; UIView *greyRect=[[UIView alloc]initWithFrame:CGRectMake(xPos, yPos, imageWidth, imageHeight)]; [greyRect setBackgroundColor:UA_NAV_CTRL_COLOR]; greyRect.layer.borderColor=[UA_NAV_BAR_COLOR CGColor]; greyRect.layer.borderWidth=1.0f; greyRect.userInteractionEnabled=YES; [greyGridArray addObject:greyRect]; [self.view addSubview:greyRect]; NSLog(@"greyGrid: %i: %@", i, greyRect); //make them touchable UITapGestureRecognizer *letterTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightLetter)]; letterTapRecognizer.numberOfTapsRequired = 1; [greyRect addGestureRecognizer:letterTapRecognizer]; }
Mani.. 111
highlightLetter:
使用参数定义目标选择器()
UITapGestureRecognizer *letterTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightLetter:)];
然后你就可以看到了
- (void)highlightLetter:(UITapGestureRecognizer*)sender { UIView *view = sender.view; NSLog(@"%d", view.tag);//By tag, you can find out where you had tapped. }
Iftikhar Ali.. 15
一年来一直在问这个问题,但仍然是某人.
声明UITapGestureRecognizer
特定视图时将标记指定为
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandlerMethod:)]; [yourGestureEnableView addGestureRecognizer:tapRecognizer]; yourGestureEnableView.tag=2;
并在您的处理程序中这样做
-(void)gestureHandlerMethod:(UITapGestureRecognizer*)sender { if(sender.view.tag == 2) { // do something here } }
tech4242.. 8
这是Swift 3的更新,也是Mani答案的补充.我建议sender.view
结合使用标记UIViews(或其他元素,取决于你想要跟踪的内容)来获得更加"先进"的方法.
将UITapGestureRecognizer添加到例如UIButton(您也可以将其添加到UIViews等)或者数组中的一大堆项目,其中包含for循环和第二个用于轻击手势的数组.
let yourTapEvent = UITapGestureRecognizer(target: self, action: #selector(yourController.yourFunction))
yourObject.addGestureRecognizer(yourTapEvent) // adding the gesture to your object
在同一个testController中定义函数(这是View Controller的名称).我们将在这里使用标签 - 标签是Int ID,您可以将其添加到UIView中yourButton.tag = 1
.如果你有一个动态的元素列表,比如一个数组你可以做一个for循环,它遍历你的数组并添加一个标签,它会逐渐增加
func yourFunction(_ sender: AnyObject) { let yourTag = sender.view!.tag // this is the tag of your gesture's object // do whatever you want from here :) e.g. if you have an array of buttons instead of just 1: for button in buttonsArray { if(button.tag == yourTag) { // do something with your button } } }
所有这一切的原因是因为当与#selector一起使用时,你不能为你的函数传递更多的参数.
如果您有一个更复杂的UI结构,并且您希望获得附加到您的点击手势的项目的父标记,您可以使用let yourAdvancedTag = sender.view!.superview?.tag
例如在UIView内获取按下按钮的UIView标记; 可用于缩略图+按钮列表等.
highlightLetter:
使用参数定义目标选择器()
UITapGestureRecognizer *letterTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightLetter:)];
然后你就可以看到了
- (void)highlightLetter:(UITapGestureRecognizer*)sender { UIView *view = sender.view; NSLog(@"%d", view.tag);//By tag, you can find out where you had tapped. }
这是Swift 3的更新,也是Mani答案的补充.我建议sender.view
结合使用标记UIViews(或其他元素,取决于你想要跟踪的内容)来获得更加"先进"的方法.
将UITapGestureRecognizer添加到例如UIButton(您也可以将其添加到UIViews等)或者数组中的一大堆项目,其中包含for循环和第二个用于轻击手势的数组.
let yourTapEvent = UITapGestureRecognizer(target: self, action: #selector(yourController.yourFunction))
yourObject.addGestureRecognizer(yourTapEvent) // adding the gesture to your object
在同一个testController中定义函数(这是View Controller的名称).我们将在这里使用标签 - 标签是Int ID,您可以将其添加到UIView中yourButton.tag = 1
.如果你有一个动态的元素列表,比如一个数组你可以做一个for循环,它遍历你的数组并添加一个标签,它会逐渐增加
func yourFunction(_ sender: AnyObject) { let yourTag = sender.view!.tag // this is the tag of your gesture's object // do whatever you want from here :) e.g. if you have an array of buttons instead of just 1: for button in buttonsArray { if(button.tag == yourTag) { // do something with your button } } }
所有这一切的原因是因为当与#selector一起使用时,你不能为你的函数传递更多的参数.
如果您有一个更复杂的UI结构,并且您希望获得附加到您的点击手势的项目的父标记,您可以使用let yourAdvancedTag = sender.view!.superview?.tag
例如在UIView内获取按下按钮的UIView标记; 可用于缩略图+按钮列表等.
一年来一直在问这个问题,但仍然是某人.
声明UITapGestureRecognizer
特定视图时将标记指定为
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandlerMethod:)]; [yourGestureEnableView addGestureRecognizer:tapRecognizer]; yourGestureEnableView.tag=2;
并在您的处理程序中这样做
-(void)gestureHandlerMethod:(UITapGestureRecognizer*)sender { if(sender.view.tag == 2) { // do something here } }