如何做出一条沿着轨迹慢慢显示的箭头
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何做出一条沿着轨迹慢慢显示的箭头相关的知识,希望对你有一定的参考价值。
最简单的,PPT就可以做:在形状里找个你喜欢的箭头或者直接拖入箭头图片,右键自定义动画,在“自定义动画”面板里点“添加效果”下拉菜单,选择“动作路径(=轨迹)”里面有很多预置路径,你也可以“绘制自定义路径”;至于怎么慢慢显示,你就在“添加效果”下拉菜单里的“进入”里慢慢选择吧。其他的,PS也可以做。当然,很多视频特效软件更不在话下。 参考技术A “自定义动画”中,选择进入的方式为“擦除”,然后方向选择从箭头的起始方向即可。比如箭头线是从右向左前行显示,就选择“自右侧”,就可以看到你要的效果了。沿着滑动路径用箭头在滑动手指上画线
【中文标题】沿着滑动路径用箭头在滑动手指上画线【英文标题】:Draw line on Swiping Finger with Arrow following the Swipe path 【发布时间】:2013-09-04 01:44:25 【问题描述】:我正在创建一个应用程序,当我在屏幕上滑动手指时,我正在使用代码画线。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), startPoint.x, startPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), endPoint.x, endPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
我也在使用代码同时在该行移动箭头....
-(void)moveBallConstantly
[UIView animateWithDuration:0.01f animations: ^
[appDel.ballImageView setCenter:CGPointMake(appDel.ballImageView.center.x + (x/increamentFraction), appDel.ballImageView.center.y + (y/increamentFraction))];
];
它只是功能的一小部分。我可以不断移动箭头,但为了更好地平滑箭头移动,我使用计时器 .01 重复调用此函数。
由于我同时进行这两项处理,因此有时会产生问题。有时箭头移动方法会延迟,有时线条绘制方法会延迟。
【问题讨论】:
【参考方案1】:我会报废计时器并将其移至touchesMoved
。
我的解决方案可让您在 UIImageView 画布上绘图,并在您绘图时让一个白框跟随您的手指。将其放入任何 UIView 中进行试用:
// These should probably be @properties
static CGPoint lastPoint;
static CGPoint currentPoint;
static UIView *box;
static UIImageView *canvas;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self];
lastPoint = [touch locationInView:self];
// Create the canvas the first time. Should probably do this elsewhere
// but done here for paste-ability
if (canvas == nil)
canvas = [[UIImageView alloc] initWithFrame:self.frame];
canvas.backgroundColor = [UIColor redColor];
[self addSubview:canvas];
// Create the box that follows the finger. Should probably do this elsewhere
// but done here for paste-ability
if (box == nil)
box = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
box.backgroundColor = [UIColor whiteColor];
[self addSubview:box];
// Ensure we can see it and move it right away
box.alpha = 1.0f;
box.center = CGPointMake(currentPoint.x, currentPoint.y - 50);
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self];
// Set up everything for drawing
UIGraphicsBeginImageContext(canvas.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[canvas.image drawInRect:CGRectMake(0, 0, canvas.image.size.width, canvas.image.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 1);
CGContextSetStrokeColorWithColor (context, [UIColor blueColor].CGColor);
// Draw the path
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);
canvas.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Animate the box very quickly to the new location
[UIView animateWithDuration:0.1f
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^
box.center = CGPointMake(box.center.x + (currentPoint.x - lastPoint.x),
box.center.y + (currentPoint.y - lastPoint.y));
completion:nil];
// Remember our last touch
lastPoint = currentPoint;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
// Finish it off by fading out the box
[UIView animateWithDuration:0.4f animations:^
box.alpha = 0.0f;
];
【讨论】:
它会随着手指移动画线,但我需要同时移动箭头在线,所以你不要认为他们会互相妥协。对于移动箭头,我使用的是每 0.01 秒调用一次的计时器。 知道了,原始问题没有提到保持箭头在线 :) 要做到这一点,只需将 box.center 设置为等于 lastPoint。如果你不想要轻微的动画,你可以去掉 touchesMoved:withEvent: 中的动画块。以上是关于如何做出一条沿着轨迹慢慢显示的箭头的主要内容,如果未能解决你的问题,请参考以下文章