简体   繁体   English

当我在按钮外触摸并在按钮上拖动时,如何使UIButton获得功能?

[英]How can I make a UIButton get a function when I touch outside the button and drag over the button?

If I had a label and a button If i touch outside the button and swiped over the button then an event should be fired to display "hello" in the label field. 如果我有一个标签和一个按钮如果我在按钮外部触摸并在按钮上滑动,则应触发一个事件以在标签字段中显示“ hello”。 tell me how can I do it? 告诉我该怎么办?

Please do following steps. 请执行以下步骤。

  1. Take UILabel and UIButton in your XIB file. 在您的XIB文件中获取UILabel和UIButton。

  2. Take UILabel and UIButton as IBOutlet in your ViewController's .h file and take property of this two described below. 在ViewController的.h文件中,将UILabel和UIButton用作IBOutlet,并采用下面所述的这两个属性。

      UILabel *lbl; UIButton *btn; @property(nonatomic, retain)IBOutlet UILabel *lbl; @property(nonatomic, retain)IBOutlet UIButton *btn; 
  3. Now set UILabel and UIButton's New Reference outlet from XIB and set UIButton's TouchUpInside link to btnClicked method. 现在,从XIB设置UILabel和UIButton的New Reference插座,并将UIButton的TouchUpInside链接设置为btnClicked方法。

  4. Declare this method in your .h file and implement it in your .m file for button event. 在您的.h文件中声明此方法,并在.m文件中实现此按钮事件。 Write this in your .h file 将其写到您的.h文件中

      -(IBAction)btnClicked:(id)sender; 

    and place this code in your .m file 并将此代码放在您的.m文件中

      -(IBAction)btnClicked:(id)sender { lbl.text = @"Hello"; } 

This is simple... 这很简单...

UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // init a button 

[button addTarget:self action:@selector(buttonDraggedOrTouchedOutSide:) 
 forControlEvents:UIControlEventTouchDragEnter|UIControlEventTouchDragInside
 |UIControlEventTouchUpOutside];

[self.view addSubview:button]; // add button to view



-(void)buttonDraggedOrTouchedOutSide:(id)sender {
    //Do your stuff
}

Thats it..Now you can catch drag and touch outside events... 就是这样。现在您可以拖动并触摸外部事件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM