Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dragging UIView under finger [duplicate]

I want to tap on a UIView and drag and have that view follow my finger, simple enough. But the easiest way of doing this is by setting the objects center to where the tap happened (Which is not what I want), I want it to move as if you grabbed the object wherever you tapped it.

There was a very useful way of doing this and it was reference in one of the iTunes U videos. The script didn't use deltaX, deltaY for dragging the image underneath where you tapped on it instead of having it center underneath your finger but I can't remember what that code was!

Does anyone have a reference to this code? Or perhaps have an efficient way of moving UIViews under a finger without having the uiview.center = tap.center concept?

like image 610
Parad0x13 Avatar asked Feb 12 '11 20:02

Parad0x13


2 Answers

The following code is an example of a simple gesture recognizer that will allow panel / view movement. Instead of modifying the center, you're modifying the origin point [basically by setting a new frame for the target view].

You can optimize this in your situation so you're not having to drill down into the gesture.view... etc

-(void)dragging:(UIPanGestureRecognizer *)gesture
{
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        //NSLog(@"Received a pan gesture");
        self.panCoord = [gesture locationInView:gesture.view];


    }
    CGPoint newCoord = [gesture locationInView:gesture.view];
    float dX = newCoord.x-panCoord.x;
    float dY = newCoord.y-panCoord.y;

gesture.view.frame = CGRectMake(gesture.view.frame.origin.x+dX, gesture.view.frame.origin.y+dY, gesture.view.frame.size.width, gesture.view.frame.size.height);
 }

Swift 4:

@objc func handleTap(_ sender: UIPanGestureRecognizer) {
        if(sender.state == .began) {
            self.panCoord = sender.location(in: sender.view)
        }

        let newCoord: CGPoint = sender.location(in: sender.view)

        let dX = newCoord.x - panCoord.x
        let dY = newCoord.y - panCoord.y

        sender.view?.frame = CGRect(x: (sender.view?.frame.origin.x)!+dX, y: (sender.view?.frame.origin.y)!+dY, width: (sender.view?.frame.size.width)!, height: (sender.view?.frame.size.height)!)
    }
like image 110
binary_falcon Avatar answered Nov 13 '22 23:11

binary_falcon


Here is the code from Apple's MoveMe project the key is to do this in the touchesMoved method. It allows the UIView(PlacardView) to see the touch and move wherever the user's touch goes. Hope this helps.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];

// If the touch was in the placardView, move the placardView to its location
if ([touch view] == placardView) {
    CGPoint location = [touch locationInView:self];
    placardView.center = location;
    return;
   }
}
like image 30
tony.stack Avatar answered Nov 14 '22 00:11

tony.stack