翻译Apple Sample"复杂的多点触控处理"编码到Swift

时间:2017-01-30 14:34:41

标签: ios objective-c swift cfdictionary

This website描述了我想要在“处理复杂的多点触控序列”下实现的目标。部分。不幸的是,它在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
}

0 个答案:

没有答案