n和uilabel的UILongPressGestureReco

n和uilabel的UILongPressGestureReco

本文介绍了uibutton和uilabel的UILongPressGestureRecognizer问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个uibutton和1个标签,longpressgesture受限于这些控件.当在任何控件上进行longpress时,下面如何获取进行longpress的对象就是我编写的代码.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[btn addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
//[self.view addSubview:btn];
btn.userInteractionEnabled = YES;

// add it
[self.view addSubview:btn];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                           initWithTarget:self
                                           action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[btn addGestureRecognizer:longPress];

下面是在longpress上调用的功能

-(void)handleLongPress:(id)sender{
 }

如果我打印了发件人的说明,那么我会得到

 <UILongPressGestureRecognizer: 0x6aa4480; state = Began; view = <UIRoundedRectButton 0x6aa9570>; target= <(action=handleLongPress:, target=<ViewController 0x6a8cc60>)>>

从中我如何获得longpress事件发生时对象的引用我的意思是我怎么知道我是否已经使用过UiLabel或Uibutton?

解决方案

只需检查UIGestureRecognizer(父类)的视图属性:

i have 2 uibutton and 1 label and longpressgesture is bounded to these control. when longpress is taken place on any control then how to get the object on which longpress is taken place below is the code that i have written.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[btn addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
//[self.view addSubview:btn];
btn.userInteractionEnabled = YES;

// add it
[self.view addSubview:btn];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                           initWithTarget:self
                                           action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[btn addGestureRecognizer:longPress];

below is function that is called on longpress

-(void)handleLongPress:(id)sender{
 }

if i printing description of sender then i get

 <UILongPressGestureRecognizer: 0x6aa4480; state = Began; view = <UIRoundedRectButton 0x6aa9570>; target= <(action=handleLongPress:, target=<ViewController 0x6a8cc60>)>>

from it how can i get the refrence of object on whcih longpress event takes placei mean how do i know whether i preessed UiLabel or Uibutton?

解决方案

Just check the UIGestureRecognizer's (the parent class) view property:

这篇关于uibutton和uilabel的UILongPressGestureRecognizer问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:48