iOS 按钮拖动。

-(void)testMove

{

    moveBtn = [[UIButton alloc ]init];

    moveBtn.frame = CGRectMake(0, 30, 60, 60);

    moveBtn.backgroundColor = [UIColorredColor];

    //[moveBtn addTarget:self action:@selector(moveBtn) forControlEvents:UIControlEventTouchDown];

    //[moveBtn addTarget:self action:@selector(moveBtnEnd) forControlEvents:UIControlEventTouchUpInside];

    

    [moveBtnaddTarget:selfaction:@selector(dragMoving:withEvent: )forControlEvents: UIControlEventTouchDragInside];

    [moveBtnaddTarget:selfaction:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:moveBtn];

}

- (void) dragMoving: (UIButton *)btn withEvent:(UIEvent *)event

{

    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];

    CGFloat x = point.x;

    CGFloat y = point.y;

    

    CGFloat btnx = btn.frame.size.width/2;

    CGFloat btny = btn.frame.size.height/2;

    

    if(x<=btnx)

    {

        point.x = btnx;

    }

    if(x >= self.view.bounds.size.width - btnx)

    {

        point.x = self.view.bounds.size.width - btnx;

    }

    

    NSLog(@"fs:%f %f",x, btnx);

    btn.center = point;

    NSLog(@"%f,,,%f",btn.center.x,btn.center.y);

}

原文地址:https://www.cnblogs.com/qingjoin/p/3626293.html