UITouch touchesMoved Finger Direction and Speed - iphone

How can I get Speed and Direction of finger movements in touchmoved function?
I want to get the finger speed and finger direction and apply it in a UIView class direction movement and animation speed.
I read this link but I can not understand the answer, in addition it is not explaining how I can detect the direction:
UITouch movement speed detection
so far, I tried this code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *anyTouch = [touches anyObject];
CGPoint touchLocation = [anyTouch locationInView:self.view];
//NSLog(#"touch %f", touchLocation.x);
player.center = touchLocation;
[player setNeedsDisplay];
self.previousTimestamp = event.timestamp;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
CGPoint prevLocation = [touch previousLocationInView:self.view];
CGFloat distanceFromPrevious = [self distanceBetweenPoints:location :prevLocation];
NSTimeInterval timeSincePrevious = event.timestamp - previousTimestamp;
NSLog(#"diff time %f", timeSincePrevious);
}

Direction will be determined from the values of "location" and "prevLocation" in touchesMoved. Specifically, location will contain the new point of the touch. For example:
if (location.x - prevLocation.x > 0) {
//finger touch went right
} else {
//finger touch went left
}
if (location.y - prevLocation.y > 0) {
//finger touch went upwards
} else {
//finger touch went downwards
}
Now touchesMoved will get called many times for a given finger movement. It will be key to your code to compare an initial value when the finger first touches the screen, with a value of the CGPoint when the movement is finally complete.

why not just the below as a variation on obuseme's response
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *aTouch = [touches anyObject];
CGPoint newLocation = [aTouch locationInView:self.view];
CGPoint prevLocation = [aTouch previousLocationInView:self.view];
if (newLocation.x > prevLocation.x) {
//finger touch went right
} else {
//finger touch went left
}
if (newLocation.y > prevLocation.y) {
//finger touch went upwards
} else {
//finger touch went downwards
}
}

Related

UITouch events calls / works only once. not works second time,

In my application i have added a google maps on my view.
I want to know the touch point on the mapView / myView, where did i taped
So that i implemented logic as follows.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:mapView_];
CGPoint touchPoint2 = [[touches anyObject] locationInView:self.view];
NSLog(#"========== %#", NSStringFromCGPoint(touchPoint));
NSLog(#"========== %#", NSStringFromCGPoint(touchPoint2));
//UITouch *touch = [[event allTouches] anyObject];
//CGPoint touchPoint = [touch locationInView:touch.view];
}
But it works only for the first touch, from the next touch event this method doesn't calls.
I didn't get what was the problem is
You can't get the touch loctaion on a map,when we touch it ,it becomes zoom,the touch events is on the UIView
with the help of this i get a log at everytime i touch on mapview with exact touch points
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.viewMap];
NSLog(#"==========1 %#", NSStringFromCGPoint(touchPoint));
}

With touches - how to stop UIIMageView moving when reaching a certain Y point

I have an image at the bottom of the screen.
I want the user to to move the image upwards until it reaches a certain Y point as follows:
[door setCenter:CGPointMake(160,347)];
So far, as you drag the image (door) upwards it continues past my destination point but when you let go it snaps back to the correct position.
How do I stop the image moving when reaching a certain point if the user's finger is still swiping upwards? Would it be inside an if statement?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (startPoint.y < 347) {
// something in here ?????
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *myTouch = [touches anyObject];
startPoint = [myTouch locationInView:self.view];
NSLog(#"position = %f and %f",startPoint.x,startPoint.y);
[door setCenter:CGPointMake(160, startPoint.y)];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[door setCenter:CGPointMake(160,347)];
}
How about setting it in touchesMoved method. Something like,
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *myTouch = [touches anyObject];
startPoint = [myTouch locationInView:self.view];
NSLog(#"position = %f and %f",startPoint.x,startPoint.y);
if (startPoint.y < 347) { //or suitable condition to verify your case
[door setCenter:CGPointMake(160, startPoint.y)]; //then only set the center
} else
{
[door setCenter:CGPointMake(160,347);
}
}

Restrict position of imageView within other imageview in iphone

I have 2 image views namely mainImageView & smallImageView. My smallImageView is added as subview to mainImageView. My smallImageView is draggable (i.e moving around the view). I want to restrict the movement of smallImageView within mainImageView (my smallImageView should not go outside of mainImageView).My smallImageView contains circle as image. Here is my code
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
smallImageView.center = location;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (location.x < mainImageView.frame.origin.x || location.y < mainImageView.frame.origin.y) {
[self touchesBegan:touches withEvent:event];
}
}
How can I fixed this problem. Thanks.
In both touch methods,
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(CGRectContainsPoint(mainImageView.frame, location)) {
smallImageView.center = location;
}
The disadvantage to this approach is that the small view moves to wherever you touch even if your touch doesn't start on the small view.
If you want to avoid this, set a 'dragging' boolean state only if your touchesBegan starts in the small view, and only respond to the touchesMoved if the dragging boolean is YES. Set dragging to NO on touchesEnded.
EDIT: Main view is a circle.
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
CGFloat xdifference = mainImageView.center.x - location.x;
CGFloat ydifference = mainImageView.center.y - location.y;
CGFloat distance = sqrt(xdifference * xdifference + ydifference * ydifference);
CGFloat radius = mainImageView.frame.size.width / 2;
if(distance < radius) {
smallImageView.center = location;
}

Drag View from the point you touch it iPhone

I know how to drag a view or object:
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
ViewMain.center = location;
}
}
but I would like to start dragging that view or image from the point where I touched it. for example if I drag (I highlighted the view and placed blue on the cursor):
the moment I start dragging if I drag the mouse 20 px to the right for example then I would like the view to also drag 20 px instead of:
maybe I have to change the center of the view to the point where I fist touch it. How could I do that?
In other words I would like to do something like when you drag the apps on the iPad:
float startX = 0;
float startY = 0;
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
startX = location.x - ViewMain.center.x;
startY = ViewMain.center.y;
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
location.x =location.x - startX;
location.y = startY;
ViewMain.center = location;
}
}

Identifying pinch gesture drag gesture on iPhone?

I have a small image on a view. The view is the object process multi-touch actions. If a finger drag on the view, the image will translate its position. And if user use 2 fingers to make pinch gesture, the image will scale its size. And I do work as section code below:
//touch detect methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch began");
//Devide into 2 cases: 1 touch and 2 touches.
if ([touches count] == 1) {
NSLog(#"Touch began cout = 1");
currentImageCenter = focusImage.center;
UITouch *touch = [[touches allObjects] objectAtIndex:0];
previousPoint = [touch locationInView:self];
isTwoFingerTouching = FALSE;
}
else if([touches count] == 2){
NSLog(#"Touch began cout = 2");
UITouch *touch = [[touches allObjects] objectAtIndex:0];
beginFirstPoint = [touch locationInView:self];
touch = [[touches allObjects] objectAtIndex:1];
beginSecondPoint = [touch locationInView:self];
isTwoFingerTouching = TRUE;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] == 1 && isTwoFingerTouching == FALSE) {
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self];
//Calculate distance
double deltaX = currentPosition.x - previousPoint.x;
double deltaY = currentPosition.y - previousPoint.y;
NSLog(#"Touch move detect 1 touch ");
focusImage.center = CGPointMake(currentImageCenter.x+deltaX, currentImageCenter.y+deltaY);
}
else if([touches count] == 2){
NSLog(#"Touch move detect 2 touches");
CGPoint currentFirstPoint;
CGPoint currentSecondPoint;
UITouch *touch = [[touches allObjects] objectAtIndex:0];
currentFirstPoint = [touch locationInView:self];
CGPoint previousFirstPoint = [touch previousLocationInView:self];
touch = [[touches allObjects] objectAtIndex:1];
currentSecondPoint = [touch locationInView:self];
CGPoint previousSecondPoint = [touch previousLocationInView:self];
//Compare previous points with current points.
//Pinch gesture
CGFloat beginDistance = distanceBetweenPoints(previousFirstPoint, previousSecondPoint);
CGFloat currentDistance = distanceBetweenPoints(currentFirstPoint, currentSecondPoint);
if (currentDistance > 0 && beginDistance > 0) {
double scale = currentDistance/beginDistance;
NSLog(#"%f", scale);
//Rotation
CGPoint vector1 = CGPointMake(previousFirstPoint.x - previousSecondPoint.x, previousFirstPoint.y - previousSecondPoint.y);
CGPoint vector2 = CGPointMake(currentFirstPoint.x - currentSecondPoint.x, currentFirstPoint.y - currentSecondPoint.y);
//[vector1, vector2].
double zValue = vector1.x*vector2.y - vector1.y*vector2.x;
CGFloat rotateAngle = angleBetweenLines(previousFirstPoint, previousSecondPoint, currentFirstPoint, currentSecondPoint);
//zValue < 0, vector1 rotate counter-clockwise, so the angle should be negative.
if (zValue < 0) {
rotateAngle = -rotateAngle;
}
//Don't allow to zoom out if the image is too small
if (scale > 1 || focusImage.frame.size.width > 30) {
CGAffineTransform previousTransform = focusImage.transform;
CGAffineTransform mixTransform = CGAffineTransformConcat(CGAffineTransformMakeScale(scale, scale), CGAffineTransformMakeRotation(rotateAngle));
focusImage.transform = CGAffineTransformConcat(previousTransform, mixTransform);
}
}
}
}
But the problem is: I can't identify whether 1 finger or 2 fingers on screen. When I touch 2 fingers, and I move (for rotate and scale) those fingers, [touches count] in touchesMoved: method still equal 1 occasionally. Any one experienced this, please tell me how to solve my problem?
Any reason why you are not using UIGestureRecognizers? It will save you a lot of work if you just use a UIPanGestureRecognizer and a UIPinchGestureRecognizer.
In any case, iOS has always behaved like that. Unless you touch with both fingers at the exact same millisecond, you will detect a one finger touch first then detect two finger touch. You need to employ some sort of a cancelling mechanism in your code if you still want to go the -touchesBegan..., -touchesMoved..., etc., method.