Reputation: 37
How will I go about touches moved finding distance when dragging my finger. Say my finger started at point A and goes to point B which is 10 pixels and goes back to point A which will be 20 pixels all together. How will I calculate that?
Upvotes: 1
Views: 2770
Reputation: 1953
All the answers so far do not consider multitouch. Those solutions become a bit more difficult if you need to remember which finger corresponds to which drag in order to support multitouch.
If you are just interested in deltas, say, you are scrolling two different things at the same time with two fingers, you can compute the delta directly from the touch object in touchesMoved
, and forget about remembering any state in touchesBegan
,
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let a = t.location(in: view)
let b = t.previousLocation(in: view)
let delta = b.y - a.y
doSomethingWithDelta()
}
}
For instance, if you have a series of SKNodes that you want to move up and down,
let nodesForFirstFinger = children.filter { $0.name == "firstGuys" }
let _ = nodesForFistFinger.map { $0.position.y += delta }
Upvotes: 0
Reputation: 819
Whole solution in Swift:
var startLocation: CGPoint?
var totalDistance: CGFloat = 0
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
if let touch = touches.first {
totalDistance = 0
startLocation = touch.location(in: view)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
if let startLocation = startLocation, let touch = touches.first {
let currentLocation = touch.location(in: view)
let dX = currentLocation.x - startLocation.x
let dY = currentLocation.y - startLocation.y
self.totalDistance += sqrt(pow(dX, 2) + pow(dY, 2))
self.startLocation = currentLocation
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesEnded(touches, with: event)
print("Total distance: \(totalDistance)")
}
Upvotes: 4
Reputation: 6427
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
point1 = [touch locationInView:self];//(point2 is type of CGPoint)
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
point2 = [touch locationInView:self];//(point2 is type of CGPoint)
}
after difference of CGPOint x or y axis you can get the difference
Upvotes: 4
Reputation: 9101
You can track the point user have moved. Remember the point in touchesBegin:
, accumulate the distance in touchesMoved:
and get the whole distance in touchesEnded:
.
– touchesBegan:withEvent:
– touchesMoved:withEvent:
- touchesEnded:withEvent:
Upvotes: 0
Reputation: 20541
for your requirement system provide main 3 delegate method for get Touch point from Device screen these 3 methods are..
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@\"touchesBegan\");// here you can find out A point which user began to touch screen
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@\"touchesMoved\");// here get points of user move finger on screen to start to end point
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@\"touchesEnded\");// here user end touch or remove finger from Device Screen means (B)
}
i hope this is useful to you...
Upvotes: 0