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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。