这篇文章通过实例实现了一个类似小米手势遥控器的功能页面。
效果图如下所示:
触摸事件的响应通过对系统的触摸实践监听来进行。
通过一个数组来对点的集合进行缓存和分析。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.allowsInteraction) return;
UITouch *touch = [touches anyObject];
CGPoint start = [touch locationInView:self.view];
[_gestureManager beginMonitorWithPoint:start];
[self showLightAtPoint:start];
NSLog(@"touch begin");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.allowsInteraction) return;
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
__weak typeof(&*self) weakSelf = self;
[_gestureManager updateMonitorWithPoint:point action:^{
[weakSelf showLightAtPoint:point];
}];
}
在触摸开始和移动的时候,通过一个类来对手势相关方法的触发和管理及其他行为。即成员_gestureManager。
- (void)beginMonitorWithPoint:(CGPoint)point
{
[self addPoint:point];
}
- (void)updateMonitorWithPoint:(CGPoint)point action:(dispatch_block_t)actionBlock
{
_curTime++;
int delta = (int)(_curTime - _lastSpawnTime);
if (delta >= TIME_GAP) {
if (actionBlock) {
actionBlock();
}
_lastSpawnTime = _curTime;
[self addPoint:point];
}
}
在开始监听后,我们不需要把系统传递的每个点都进行触发贴图显示点的轨迹,所以设置了成员来设置间隙位,已达到对点的密集程度进行控制。
<