首页 > 代码库 > IOS拖动
IOS拖动
#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
IOS拖动