http://blog.csdn.net/qq5306546/article/details/8083402
当点击Button后,拖动到屏幕上的其它位置,Button会根据移动的方法位置发生变化
- #import "ViewController.h"
-
- @interface ViewController ()
-
- @property (nonatomic, strong) UIButton *btn;
-
- @end
-
- @implementation ViewController
- @synthesize btn;
-
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- self.btn.frame = CGRectMake(10, 10, 50, 50);
-
- [self.btn setTitle:@"触摸" forState:UIControlStateNormal];
- [self.btn setTitle:@"移动" forState:UIControlEventTouchDown];
- [self.btn addTarget:self action:@selector(dragMoving:withEvent: )forControlEvents: UIControlEventTouchDragInside];
- [self.btn addTarget:self action:@selector(dragEnded:withEvent: )forControlEvents: UIControlEventTouchUpInside |
- UIControlEventTouchUpOutside];
-
- [self.view addSubview:self.btn];
- }
-
-
- - (void) dragMoving: (UIControl *) c withEvent:ev
- {
- c.center = [[[ev allTouches] anyObject] locationInView:self.view];
- }
-
- - (void) dragEnded: (UIControl *) c withEvent:ev
- {
- c.center = [[[ev allTouches] anyObject] locationInView:self.view];
- }
-
- @end