uibutton和uilabel的UILongPressGestureRecognizer问题 [英] UILongPressGestureRecognizer issue with uibutton and uilabel

查看:73
本文介绍了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(父类)的视图属性:

@property(非原子,只读)UIView *视图

手势识别器所附的视图. (只读)

@property(非原子,只读)UIView * view 讨论 您可以使用addGestureRecognizer:方法将手势识别器附加(或添加)到UIView对象.

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 place i mean how do i know whether i preessed UiLabel or Uibutton?

解决方案

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

@property(nonatomic, readonly) UIView *view

The view the gesture recognizer is attached to. (read-only)

@property(nonatomic, readonly) UIView *view Discussion You attach (or add) a gesture recognizer to a UIView object using the addGestureRecognizer: method.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆