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

被刻印的时光 ゝ 提交于 2019-12-23 05:35:11

问题


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.


回答1:


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];
        }
}



回答2:


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];
    }
}



回答3:


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.



来源:https://stackoverflow.com/questions/11650149/drag-able-button-in-ios-iphone-such-that-it-repositions-itself-in-its-orignal-po

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!