简体   繁体   English

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

[英]How to create and draw a visual swipe gesture

I'd like to try implementing a visual swipe for an iPhone project, like they do in some games, like Fruit Ninja. 我想尝试为iPhone项目实现可视化滑动,就像它们在《水果忍者》等某些游戏中所做的那样。 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. 我可以看到使用-touchesMoved生成新点,并使用NSMutableArray跟踪这些点。 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? 我会做一个CALayer并画一条线连接活动点吗? Or use some other view object and join them together at the points... 或使用其他视图对象,然后将它们结合在一起。

Any ideas? 有任何想法吗?

Something like this would work, if you had populated 'points' with CGPoints. 如果您用CGPoints填充了“点”,则类似的事情将起作用。 Caveat: this is a quick cut, paste and edit job - so there will probably be errors. 注意:这是一个快速剪切,粘贴和编辑的工作-因此可能会出现错误。 Also, I use stl::vector for 'points'. 另外,我将stl :: vector用于“点”。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM