IOS可以拖动的UIButton

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  5.     self.btn.frame = CGRectMake(10, 10, 50, 50);  
  6.   
  7.     [self.btn setTitle:@"触摸" forState:UIControlStateNormal];  
  8.     [self.btn setTitle:@"移动" forState:UIControlEventTouchDown];  
  9.     [self.btn addTarget:self action:@selector(dragMoving:withEvent: )forControlEvents: UIControlEventTouchDragInside];  
  10.     [self.btn addTarget:self action:@selector(dragEnded:withEvent: )forControlEvents: UIControlEventTouchUpInside |  
  11.      UIControlEventTouchUpOutside];  
  12.       
  13.     [self.view addSubview:self.btn];  
  14. }  
  15.   
  16.   
  17. - (void) dragMoving: (UIControl *) c withEvent:ev  
  18. {  
  19.     c.center = [[[ev allTouches] anyObject] locationInView:self.view];  
  20. }  
  21.   
  22. - (void) dragEnded: (UIControl *) c withEvent:ev  
  23. {  
  24.     c.center = [[[ev allTouches] anyObject] locationInView:self.view];  
posted @ 2015-10-27 10:10  onlytyj  阅读(191)  评论(0)    收藏  举报