- - (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];
- }
posted @
2015-10-27 10:10
onlytyj
阅读(
191)
评论()
收藏
举报