WWDC 2015会议233 - iOS:https://developer.apple.com/videos/play/wwdc2015/233/?time=74
我可以找到苹果的演示代码,但在我的生活中找不到任何关于[UIEvent predictedTouchesForTouch:]的实际文档。
它应该是一个iOS 9 API。这个API是活动的,只是还没有文档化吗?我可以看到他们将其添加到Swift中的API差异,但仍然没有文档。
发布于 2016-02-29 01:47:57
是的,预测触点的文档(就像合并后的触点的文档一样)很少。在这一点上(Xcode 7.2和7.3 Beta 4),我看到的唯一引用(在WWDC视频之外,您所引用的以及散布在web上的其他演示)是在标题中,它将预测的触摸描述如下:
一个用于触摸事件的辅助
UITouch数组,预测在给定的主触摸中会发生这些事件。这些预测可能与触摸的真实行为不完全匹配,因此它们应该被解释为一种估计。
但是它的工作原理与你提到的WWDC视频中所描述的完全一样,无论是Swift还是Objective。
注意,您可能不会在模拟器上看到预测的和合并的触摸,但是如果您使用一个有能力的设备,您会看到的。在使用我的iPhone 6+时,我偶尔会看到预期的触摸,但在iPad Pro上会看到它们的一致性(如果您在iPad Pro上使用苹果铅笔,则会看到更多的触摸)。
为了说明合并和预测的触摸,下面是一个Objective演示:
@interface ViewController ()
@property (nonatomic, strong) UIBezierPath *path;
@property (nonatomic, weak) CAShapeLayer *pathLayer;
@property (nonatomic, weak) CAShapeLayer *predictedPathLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// set up layer for the main (non-predicted) path; line blue line
CAShapeLayer *mainPathLayer = [CAShapeLayer layer];
mainPathLayer.lineWidth = 1;
mainPathLayer.strokeColor = [UIColor blueColor].CGColor;
mainPathLayer.fillColor = [UIColor clearColor].CGColor;
mainPathLayer.lineCap = kCALineCapRound;
mainPathLayer.lineJoin = kCALineJoinRound;
mainPathLayer.frame = self.view.layer.bounds;
[self.view.layer addSublayer:mainPathLayer];
self.pathLayer = mainPathLayer;
// set up layer for the predicted path, if any; thicker red line (just so I can see it clearly)
CAShapeLayer *predictedPathLayer = [CAShapeLayer layer];
predictedPathLayer.lineWidth = 5;
predictedPathLayer.strokeColor = [UIColor redColor].CGColor;
predictedPathLayer.fillColor = [UIColor clearColor].CGColor;
predictedPathLayer.lineCap = kCALineCapRound;
predictedPathLayer.lineJoin = kCALineJoinRound;
predictedPathLayer.frame = self.view.layer.bounds;
[self.view.layer addSublayer:predictedPathLayer];
self.predictedPathLayer = predictedPathLayer;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
self.path = [UIBezierPath bezierPath];
[self.path moveToPoint:[touch locationInView:self.view]];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
// handle coalesced touches
NSArray<UITouch *> *coalescedTouches = [event coalescedTouchesForTouch:touch];
for (UITouch *coalescedTouch in coalescedTouches) {
location = [coalescedTouch locationInView:self.view];
[self.path addLineToPoint:location];
}
self.pathLayer.path = self.path.CGPath;
// now handle predicted touches
UIBezierPath *predictedPath = [UIBezierPath bezierPath];
[predictedPath moveToPoint:location];
NSArray<UITouch *> *predictedTouches = [event predictedTouchesForTouch:touch];
for (UITouch *predictedTouch in predictedTouches) {
[predictedPath addLineToPoint:[predictedTouch locationInView:self.view]];
}
self.predictedPathLayer.path = predictedPath.CGPath;
// for diagnostic purposes, let's log the count of each; do not do this in production app
NSLog(@"%ld %ld", [coalescedTouches count], [predictedTouches count]);
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.predictedPathLayer.path = NULL; // discard any predicted path, because that's no longer valid
}
@end如果您需要在9之前支持iOS版本,您也需要检查respondsToSelector。
Swift的例子非常相似。请参阅这篇关于光滑曲线的无关文章,它说明了如何使用Swift:Drawing class drawing straight lines instead of curved lines中的预测(和合并)触点
https://stackoverflow.com/questions/35686591
复制相似问题