如何创建和绘制视觉滑动手势

发布于 2024-09-29 20:25:56 字数 275 浏览 5 评论 0原文

我想尝试为 iPhone 项目实现视觉滑动,就像在某些游戏(例如《水果忍者》)中所做的那样。当您在屏幕上拖动手指时,它会留下一条痕迹,一段时间后就会消失。我认为“链”中可以有固定数量的点,并且当新点添加到前面时,旧点会从后面删除。我可以看到使用 -touchesMoved 来生成新点,并使用 NSMutableArray 来跟踪这些点。我只是无法想象我会使用什么方法来实际绘制这些片段。我会制作一个 CALayer 并画一条连接活动点的线吗?或者使用其他一些视图对象并将它们在点处连接在一起......

有什么想法吗?

I'd like to try implementing a visual swipe for an iPhone project, like they do in some games, like Fruit Ninja. As you drag your finger around the screen, it leaves a trail that disappears after a while. I would think that you could have a fixed number of points in the "chain" and as new points are added to the front, old ones are removed from the rear. I can see using -touchesMoved to generate new points and an NSMutableArray to keep track of the points. I just can't imaging what method I'd use to actually draw the segments. Would I make one CALayer and draw a line connecting the active points? Or use some other view object and join them together at the points...

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年不长 2024-10-06 20:25:56

如果你用 CGPoints 填充“点”,这样的事情就会起作用。警告:这是一项快速剪切、粘贴和编辑工作 - 因此可能会出现错误。另外,我使用 stl::vector 来表示“点”。您可能想使用其他结构。

CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef dataPath = CGPathCreateMutable();
bool firstPoint = YES;

for (int i=0; i < points.size(); ++i)
    {
    CGPoint point = points[i];
    if (firstPoint)
        {
        CGPathMoveToPoint(dataPath, NULL, point.x, point.y);
        firstPoint = NO;
        }
    else
        {
        CGPathAddLineToPoint(dataPath, NULL, point.x, point.y);
        }
    }

CGContextSetRGBStrokeColor( context, 1.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth( context, 5);
CGContextBeginPath( context );
CGContextAddPath( context, dataPath );
CGContextDrawPath( context, kCGPathStroke);

CGPathRelease(dataPath);

Something like this would work, if you had populated 'points' with CGPoints. Caveat: this is a quick cut, paste and edit job - so there will probably be errors. Also, I use stl::vector for 'points'. You may want to use some other structure.

CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef dataPath = CGPathCreateMutable();
bool firstPoint = YES;

for (int i=0; i < points.size(); ++i)
    {
    CGPoint point = points[i];
    if (firstPoint)
        {
        CGPathMoveToPoint(dataPath, NULL, point.x, point.y);
        firstPoint = NO;
        }
    else
        {
        CGPathAddLineToPoint(dataPath, NULL, point.x, point.y);
        }
    }

CGContextSetRGBStrokeColor( context, 1.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth( context, 5);
CGContextBeginPath( context );
CGContextAddPath( context, dataPath );
CGContextDrawPath( context, kCGPathStroke);

CGPathRelease(dataPath);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文