Apple_iOS0304
Apple_iOS0304

Reputation: 1102

Drag-able button in iOS iPhone such that it repositions itself in its orignal position after touch ended.

I am making an app where there are different buttons for different colors, if i drag this color button on a specific view its color must change to this button's color and the color button should reposition in its orignal location. I tried various methods like touches began and touches ended but it doesn't seem to solve my issue. I also tried customized methods which fire on various uicontrolstate but it didnt work as well. plz help me out with this.

Upvotes: 0

Views: 1374

Answers (3)

cloosen
cloosen

Reputation: 993

I think you use touchesBegan,touchesMoved and touchesEnded can solve the problem.

touchesBegan is to mark the orignPosition.

CGPoint orignPosition;
CGColor buttonColor;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    UIView *touchView = [touch view];
        if ([touchView isKindOfClass:[UIButton class]]) 
        {
            orignPosition = (UIButton *)touchView.center;
            buttonColor = (UIButton *)touchView.backgroundColor;
        }
}

touchesMoved is to let the button move with your finger

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    UIView *touchView = [touch view];
    CGPoint movedPoint = [touch locationInView:yourMainView];
        CGPoint deltaVector = CGPointMake(movedPoint.x - lastTouchPoint.x, movedPoint.y - lastTouchPoint.y);
    lastTouchPoint = movedPoint;

    if ([touchView isKindOfClass:[UIButton class]])
        {
        touchView.center = CGPointMake(touchView.center.x + deltaVector.x, touchView.center.y + deltaVector.y);
        }
}

touchesEnded is to judge whether change your special view`s color

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{   
    UITouch *touch = [touches anyObject];
    UIView *touchView = [touch view];
        CGPoint movedPoint = [touch locationInView:scrollView];
        if ([touchView isKindOfClass:[UIButton class]]) 
        {
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.3];
            (UIButton *)touchView.center = orignPosition;
            [UIView commitAnimations];
        }
        if(CGRectContainsPoint(specialView.frame, [touch locationInView:yourMainView]))
        {
            [yourMainView setBackgroundColor:buttonColor];
        }
}

Upvotes: 3

TheTiger
TheTiger

Reputation: 13364

I would like to suggest you use UIImageView instead of UIButton . Set [UIImageView userInractionEnabled:YES]. It would be better. You can Drag imageView easily by using touchBegin: and touchMove: methods.

Upvotes: 0

Moonkid
Moonkid

Reputation: 916

I think you don`t need to use buttons, you can create UIView, set background color to it, and add pan gesture to it. In .h file create some variables:

UIView *green;
CGRect initialPosition;

in .m file in init Method add somethig like this

initialPosition = CGRectMake(0, 0, 44, 44);
green = [[UIView alloc]initWithFrame:initialPosition];
green.backgroundColor = [UIColor greenColor];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(habdlePan:)]
[green addGestureRecognizer:green];
[panGesture release];
[self.view addSubview:green];
[green release];

HandleTap must be like:

- (void) habdlePan:(UIPanGestureRecognizer *)panGesture {
    //Move your button here
    if(gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        //All fingers are lifted.
        //Return btn to init position
        [UIView beginAnimations:nil context:nil];
        green.frame = initialPosition;
        [UIView commitAnimations];
    }
}

Upvotes: 1

Related Questions