For basic overview of Gesture Recognizer Click here
We will continue here with Pan Gesture Recognizer. Pan Gesture Recognizer allow to drag view around the screen.In this part, except for implementing the necessary code that will enable our app to support the pan gesture.
The pan gesture is actually a drag gesture. It’s used when it’s needed to drag views from one point to another.
First create a view
UIView *panView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
panView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:panView];
Defining Callback method for Gesture Recognizer
-(void) handlepanGesture:(UIPanGestureRecognizer *)panGesture{
CGPoint touchPoint = [panGesture locationInView:self.view];
panView.center = touchPoint;
}
Now run the app and check.
You can drag 'Pan View' around main View.
The approach I presented above is simple for the sake of the tutorial. In a real application, you might want to enrich the movement of the view by adding acceleration or deceleration when you start or stop dragging, or anything else you need. It’s up to you to apply the proper logic in the action method you implement.
You can find points per second the dragged view travels in both the horizontal and vertical axis while dragging by method velocityInView: .
Go to Callback method and add following codes
//find velocity
CGPoint velocity = [panGesture velocityInView:self.view];
NSLog(@"Horizontal Velocity: %.2f points/sec",velocity.x);
NSLog(@"Vertical Velocity: %.2f points/sec",velocity.y);
Now run the app again and drag panView.
you can check horizontal and vertical velocity on Console Area.
</script>
No comments:
Post a Comment