[objective-c]UIButtonをドラッグする

UIButtonをドラッグする

UIButton*btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self action:@selector(onTouchDragInside:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[self.view addSubview:btn];

-(void)onTouchDragInside:(UIButton*)btn withEvent:(UIEvent*)event{
    UITouch *touch=[[event touchesForView:btn] anyObject];
    CGPoint prevPos=[touch previousLocationInView:btn];
    CGPoint pos=[touch locationInView:btn];
    float dX=pos.x-prevPos.x;
    float dY=pos.y-prevPos.y;
    btn.center=CGPointMake(btn.center.x+dX,btn.center.y+dY);
}