合并和预测目标c中的触摸
问题描述:
我在目标C中有一个巨大的项目(一个应用程序),我想做一个严重的过渡到iOS 9.在应用程序中有很多绘图,我想使用预测和联合触摸。正如http://flexmonkey.blogspot.de/2015/09/advanced-touch-handling-in-ios9.html 描述的代码中有看起来像合并和预测目标c中的触摸
if let coalescedTouches = event.coalescedTouchesForTouch(touch)
{
print("coalescedTouches:", coalescedTouches.count)
for coalescedTouch in coalescedTouches
{
let locationInView = coalescedTouch.locationInView(view)
coalescedDrawPath.addLineToPoint(locationInView)
coalescedDrawPath.appendPath(UIBezierPath.createCircleAtPoint(locationInView, radius: 2))
coalescedDrawPath.moveToPoint(locationInView)
}
coalescedDrawLayer.path = coalescedDrawPath.CGPath
}
然而,这是斯威夫特。目标C中是否有任何直接使用这些功能的方法?我没有在网上找到任何东西。
答
这在客观C代码块是一样的东西:
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UIBezierPath *coalescedPath = [[UIBezierPath alloc]init];
[coalescedPath moveToPoint:CGPointZero];
UITouch *touch = [touches anyObject];
if ([event coalescedTouchesForTouch:touch]) {
NSArray *coalescedTouches = [NSArray arrayWithArray: [event coalescedTouchesForTouch:touch]];
NSLog(@"Coalesced Touches: %lu",(unsigned long)[coalescedTouches count]);
for (UITouch*coalescedTouch in coalescedTouches) {
CGPoint locationInView = [coalescedTouch locationInView:self.view];
[coalescedPath addLineToPoint:locationInView];
[coalescedPath appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(locationInView.x-2.0, locationInView.y-2.0, 4.0, 4.0)]];
[coalescedPath moveToPoint:locationInView];
}
[[UIColor blackColor] setStroke];
[coalescedPath stroke];
}
}
对于所有苹果的API的时刻被写入两个目标C和斯威夫特。查看UITouch和UIEvent参考指南,了解如何实施预测和合并触摸API。
+0
非常感谢。这有帮助。我根本找不到它(看上去错了) –
您在网上没有发现任何关于使用swift来扩充obj-c项目的内容吗? – Wain
为什么OP要用swift来扩充他们的obj-c项目,而在obj-c中编写代码很简单?这是一个非常合理的问题,所以我不明白为什么它被低估了。如果你看看Apple的网站,只有swift的示例代码,没有obj-c的示例代码。 – CpnCrunch