簡體   English   中英

與uibutton和uilabel的UILongPressGestureRecognizer問題

[英]UILongPressGestureRecognizer issue with uibutton and uilabel

我有2個uibutton和1個標簽,長壓力限制在這些控制之下。 當在任何控件上進行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>)>>

從中我怎么能得到長對象事件發生的對象的參考我的意思是我怎么知道我是否預言了UiLabel或Uibutton?

只需檢查UIGestureRecognizer(父類)視圖屬性:

@property(非原子,只讀)UIView *視圖

手勢識別器附加到的視圖。 (只讀)

@property(非原子,只讀)UIView *視圖討論使用addGestureRecognizer:方法將手勢識別器附加(或添加)到UIView對象。

-(void)handleLongPress:(UILongPressGestureRecognizer *)sender{


    if ([sender.view isKindOfClass:[UIButton class]]) {

            UIButton *myButton = (UIButton *)sender.view; // here is your sender object or Tapped button

            if (myButton.tag == 1) {

                    //sender is first Button. Because we assigned 1 as Button1 Tag when created.
            }
            else if (myButton.tag == 2){

                    //sender is second Button. Because we assigned 2 as Button2 Tag when created.
            }
    }

    if ([sender.view isKindOfClass:[UILabel class]]) {

        UILabel *myLabel = (UILabel *)sender.view; // here is your sender object or Tapped label.

    }


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM