Search code examples
iphoneobjective-ciosxcodeuitouch

UITouch, Drag and Drop not working at all


I have a UIView and on the top of him I've got UIButton, UITableView and UINavigationBar. what I want to do is, when you click on the UIButton and drag it, all the view will move to the left side only, until the screen ends. I mean that you can drag the view to the left and when you stop touching, the view will stop where your finger stopped touching the screen.

I can't do it, he shows me only the "Touches Began 1" and thats it. what seems to be my problem here?

Thanks alot everyone!

float oldX, oldY;
BOOL dragging;

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    NSLog("Touches Began 1");
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (CGRectContainsPoint(btnOpen.frame, touchLocation))
    {
        NSLog("Touches Began 2");
        dragging = YES;
        oldX = touchLocation.x;
        oldY = touchLocation.y;
    }
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (dragging)
    {
        NSLog("Dragging!");
        CGRect frame = mainView.frame;
        frame.origin.x = mainView.frame.origin.x + touchLocation.x - oldX;
        frame.origin.y =  mainView.frame.origin.y + touchLocation.y - oldY;
        mainView.frame = frame;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog("Touches Ended!");
    dragging = NO;
}


- (void)viewDidLoad
{    
    [btnOpen addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents: UIControlEventTouchDown];
    [btnOpen addTarget:self action:@selector(touchesMoved:withEvent:) forControlEvents: UIControlEventTouchDragInside];
    [btnOpen addTarget:self action:@selector(touchesEnded:withEvent:) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
}

Solution

  • - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
    {
        NSLog("Touches Began 1");
    for (UITouch *touch in touches) {
        //UITouch *touch = [[event allTouches] anyObject];
        CGPoint touchLocation = [touch locationInView:self.view];
    
        if (CGRectContainsPoint(btnOpen.frame, touchLocation))
        {
            NSLog("Touches Began 2");
            //dragging = YES;
            oldX = touchLocation.x;
            oldY = touchLocation.y;
        }
    }
    }
    
    - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
    {
       // UITouch *touch = [[event allTouches] anyObject];
    for (UITouch *touch in touches) {
        CGPoint touchLocation = [touch locationInView:self.view];
    
        if (CGRectContainsPoint(btnOpen.frame, touchLocation))
        {
            NSLog("Dragging!");
            CGRect frame = mainView.frame;
            frame.origin.x = mainView.frame.origin.x + touchLocation.x - oldX;
            frame.origin.y =  mainView.frame.origin.y + touchLocation.y - oldY;
            mainView.frame = frame;
        }
    }
    }
    

    Try like this i think it will be helpful to you.