该网站在“处理复杂的多点触控序列”部分描述了我想要实现的目标。不幸的是,它显示在 Objective-C 中。我可以将大部分内容翻译成 Swift,但是当涉及到几行时,比如带有 的行CFDictionary
,我真的不知道发生了什么。
如果有人可以帮助我理解/翻译清单 3-4 和 3-5 或建议另一种处理多点触控事件的方法,我将不胜感激。我已经离开 '???' 我不确定。
这是 Obj-C(存储触摸初始位置):
- (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];
}
}
}
这是检索初始位置:
- (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;
}
这是我所管理的:
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)
}
}
}
和:
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
}