简体   繁体   中英

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. tell me how can I do it?

Please do following steps.

  1. Take UILabel and UIButton in your XIB file.

  2. Take UILabel and UIButton as IBOutlet in your ViewController's .h file and take property of this two described below.

      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.

  4. Declare this method in your .h file and implement it in your .m file for button event. Write this in your .h file

      -(IBAction)btnClicked:(id)sender; 

    and place this code in your .m file

      -(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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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