This website describes what I want to achieve under the 'Handling a Complex Multitouch Sequence' section. Unfortunately, it is shown in Objective-C. I can translate most of it into Swift, but when it comes to a few lines, like the lines with CFDictionary
, I honestly have no idea what's going on.
If somebody could help me understand/translate listings 3-4 and 3-5 or suggest another method for handling multi-touch events, I would be very grateful. I have left '???' where I am not sure.
Here is the Obj-C (storing touch initial location):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self cacheBeginPointForTouches:touches];
}
- (void)cacheBeginPointForTouches:(NSSet *)touches {
if ([touches count] > 0) {
for (UITouch *touch in touches) {
CGPoint *point = (CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch);
if (point == NULL) {
point = (CGPoint *)malloc(sizeof(CGPoint));
CFDictionarySetValue(touchBeginPoints, touch, point);
}
*point = [touch locationInView:view.superview];
}
}
}
Here is retrieving the initial location:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGAffineTransform newTransform = [self incrementalTransformWithTouches:touches];
}
- (CGAffineTransform)incrementalTransformWithTouches:(NSSet *)touches {
NSArray *sortedTouches = [[touches allObjects] sortedArrayUsingSelector:@selector(compareAddress:)];
// Other code here
CGAffineTransform transform = CGAffineTransformIdentity;
UITouch *touch1 = [sortedTouches objectAtIndex:0];
UITouch *touch2 = [sortedTouches objectAtIndex:1];
CGPoint beginPoint1 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch1);
CGPoint currentPoint1 = [touch1 locationInView:view.superview];
CGPoint beginPoint2 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch2);
CGPoint currentPoint2 = [touch2 locationInView:view.superview];
// Compute the affine transform
return transform;
}
Here's what I've managed:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
cacheBeginPoint(touches: touches)
}
func cacheBeginPoint(touches: Set<UITouch>) {
if touches.count > 0 {
for touch: UITouch in touches {
var point: CGPoint? = CFDictionaryGetValue(???)
if point == nil {
point = ???
CFDictionarySetValue(???)
}
point = touch.location(in: view?.superview)
}
}
}
and:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
var newTransform: CGAffineTransform = incrementalTransform(touches: touches)
}
func incrementalTransform(touches: Set<UITouch>) -> CGAffineTransform {
var sortedTouches: NSArray = ???
var transform = CGAffineTransform.identity
var touch1: UITouch? = sortedTouches[0]
var touch2: UITouch? = sortedTouches[1]
var beginPoint1: = CFDictionaryGetValue(???)
var currentPoint1: = touch1?.location(in: view?.superview)
var beginPoint2: = CFDictionaryGetValue(???)
var currentPoint2: = touch2?.location(in: view?.superview)
return transform
}