How to use UIPanGestureRecognizer to move object? iPhone/iPad
@Tono Nam's answer (accepted) in Swift 5
func someMethod() {
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(move))
panGestureRecognizer.minimumNumberOfTouches = 1
panGestureRecognizer.maximumNumberOfTouches = 1
view.addGestureRecognizer(panGestureRecognizer)
}
@objc func move(sender: UIPanGestureRecognizer) {
guard let senderView = sender.view else { return }
self.view.bringSubviewToFront(senderView)
var translatedPoint: CGPoint = sender.translation(in: senderView.superview)
if(sender.state == .began) {
let firstX = senderView.center.x
let firstY = senderView.center.y
}
translatedPoint = CGPoint(x: senderView.center.x + translatedPoint.x, y: senderView.center.y + translatedPoint.y)
senderView.center = translatedPoint
sender.setTranslation(.zero, in: senderView)
if(sender.state == .ended) {
let velocityX: CGFloat = (0.2 * sender.velocity(in: self.view).x)
let velocityY: CGFloat = (0.2 * sender.velocity(in: self.view).y)
var finalX: CGFloat = translatedPoint.x + velocityX
var finalY: CGFloat = translatedPoint.y + velocityY
if(finalX < 0) {
finalX = 0
} else if (finalX > self.view.frame.size.width) {
finalX = self.view.frame.size.width;
}
if (finalY < 50) { // to avoid status bar
finalY = 50;
} else if (finalY > self.view.frame.size.height) {
finalY = self.view.frame.size.height;
}
let animationDuration: TimeInterval = TimeInterval(abs(velocityX) * 0.0002 + 0.2)
print("the animation duration is \(animationDuration)")
UIView.animate(withDuration: animationDuration, delay: 0, options: .curveEaseOut) {
senderView.center = CGPoint(x: finalX, y: finalY)
} completion: { completed in
// call animationDidFinish completion handler here
}
}
}
I found the tutorial Working with UIGestureRecognizers, and I think that is what I am looking for. It helped me come up with the following solution:
-(IBAction) someMethod {
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[ViewMain addGestureRecognizer:panRecognizer];
[panRecognizer release];
}
-(void)move:(UIPanGestureRecognizer*)sender {
[self.view bringSubviewToFront:sender.view];
CGPoint translatedPoint = [sender translationInView:sender.view.superview];
if (sender.state == UIGestureRecognizerStateBegan) {
firstX = sender.view.center.x;
firstY = sender.view.center.y;
}
translatedPoint = CGPointMake(sender.view.center.x+translatedPoint.x, sender.view.center.y+translatedPoint.y);
[sender.view setCenter:translatedPoint];
[sender setTranslation:CGPointZero inView:sender.view];
if (sender.state == UIGestureRecognizerStateEnded) {
CGFloat velocityX = (0.2*[sender velocityInView:self.view].x);
CGFloat velocityY = (0.2*[sender velocityInView:self.view].y);
CGFloat finalX = translatedPoint.x + velocityX;
CGFloat finalY = translatedPoint.y + velocityY;// translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
if (finalX < 0) {
finalX = 0;
} else if (finalX > self.view.frame.size.width) {
finalX = self.view.frame.size.width;
}
if (finalY < 50) { // to avoid status bar
finalY = 50;
} else if (finalY > self.view.frame.size.height) {
finalY = self.view.frame.size.height;
}
CGFloat animationDuration = (ABS(velocityX)*.0002)+.2;
NSLog(@"the duration is: %f", animationDuration);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidFinish)];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
UIPanGestureRecognizer * pan1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(moveObject:)];
pan1.minimumNumberOfTouches = 1;
[image1 addGestureRecognizer:pan1];
-(void)moveObject:(UIPanGestureRecognizer *)pan;
{
image1.center = [pan locationInView:image1.superview];
}
-(IBAction)Method
{
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[ViewMain addGestureRecognizer:panRecognizer];
[panRecognizer release];
}
- (Void)handlePan:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [recognizer velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);
float slideFactor = 0.1 * slideMult; // Increase for more of a slide
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
recognizer.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.center = finalPoint;
} completion:nil];
}
}