此文章原为网络代码段子,此处优化滑动瞬间抖动bug
因网络转载过多,找不到原作者,故没有原帖地址
按钮初始化
BOOL isMoving;
CGPoint delt;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- self.btn.frame = CGRectMake(10, 10, 50, 50);
- [self.btn setTitle:@"触摸" forState:UIControlStateNormal];
- [self.btn addTarget:self action:@selector(dragMoving:withEvent: )forControlEvents: UIControlEventTouchDragInside];
- [self.btn addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];
- self.a=0;
- [self.view addSubview:self.btn];
- }
点击方法
- -(void)doClick:(UIButton*)sender
- { if (!isMoving) { NSLog(@"1111"); } isMoving = NO; delt.x = -1; delt.y = -1;
- }
拖动方法
- - (void) dragMoving: (UIButton *) c withEvent:ev
- { NSLog(@"2222222"); isMoving = YES; UITouch *g = [[event allTouches] anyObject]; CGPoint point = [g locationInView:[self superview]]; if (delt.x == -1 && delt.y == -1) { delt.x = point.x - self.frame.origin.x - self.frame.size.width/2; delt.y = point.y - self.frame.origin.y - self.frame.size.height/2; } point.x -= delt.x; point.y -= delt.y; c.center = point; NSLog(@"%f,,,%f",c.center.x,c.center.y);
- }