这篇文章通过实例实现了一个类似小米手势遥控器的功能页面。
效果图如下所示:
触摸事件的响应通过对系统的触摸实践监听来进行。
通过一个数组来对点的集合进行缓存和分析。
- (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];
}
}
在开始监听后,我们不需要把系统传递的每个点都进行触发贴图显示点的轨迹,所以设置了成员来设置间隙位,已达到对点的密集程度进行控制。

本文介绍如何在iOS应用中实现小米遥控器类似的手势监听和UI界面。通过监听触摸事件,分析点的集合判断手势方向,如返回、功能等,并展示点轨迹。项目源代码在Rannie/MIRemoteControl,文章讨论了项目中遇到的问题,如点集合的维护、贴图方式、事件监听以及点路径分析等,邀请读者交流指正。
最低0.47元/天 解锁文章
664

被折叠的 条评论
为什么被折叠?



