gpt4 book ai didi

ios - 按照滑动路径在滑动手指上用箭头画线

转载 作者:可可西里 更新时间:2023-11-01 03:21:24 34 4
gpt4 key购买 nike

我正在创建一个应用程序,当我在屏幕上滑动手指时,我正在使用代码画线。

- (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 重复调用此函数。

因为我同时进行这两个处理,所以有时会产生问题。有时箭头移动方法会延迟,有时线绘制方法会延迟。

最佳答案

我会废弃计时器并在 touchesMoved 中移动它。

我的解决方案可让您在 UIImageView Canvas 上绘图,并在您绘图时让一个白框跟随您的手指。将它放入任何 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;
}];
}

关于ios - 按照滑动路径在滑动手指上用箭头画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18547972/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com