gpt4 book ai didi

ios - 当我改变手指速度时如何改变画笔大小

转载 作者:行者123 更新时间:2023-11-29 04:18:30 25 4
gpt4 key购买 nike

我有一个关于绘画的项目,我会用我的手指画一些东西。当我的手指移动得更快时,画笔的尺寸会更大。我该如何编码呢?​​??这是关于绘图的代码:在我的项目中,我使用 UItouch 来检查我的手指。

 (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
if (drawEnable==YES) {
NSArray * touchesArr=[[event allTouches] allObjects];
if ([touchesArr count]==1) {
NSMutableArray *arrayPointsInStroke = [NSMutableArray array];
NSMutableDictionary *dictStroke = [NSMutableDictionary dictionary];
[dictStroke setObject:arrayPointsInStroke forKey:@"points"];
[dictStroke setObject:self.currentColor forKey:@"color"];
[dictStroke setObject:[NSNumber numberWithFloat:self.currentSize] forKey:@"size"];

CGPoint point = [[touches anyObject] locationInView:self];
[arrayPointsInStroke addObject:NSStringFromCGPoint(point)];

[self.arrayStrokes addObject:dictStroke];
lastDistance=0;
}
}

}
// Add each point to points array
- (void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *) event
{
float sub_x,sub_y;
float currentDistance;
if (penStyle==1) {
CGPoint point = [[touches anyObject] locationInView:self];
CGPoint prevPoint = [[touches anyObject] previousLocationInView:self];
NSMutableArray *arrayPointsInStroke = [[self.arrayStrokes lastObject] objectForKey:@"points"];
[arrayPointsInStroke addObject:NSStringFromCGPoint(point)];

CGRect rectToRedraw = CGRectMake(\
((prevPoint.x>point.x)?point.x:prevPoint.x)-currentSize,\
((prevPoint.y>point.y)?point.y:prevPoint.y)-currentSize,\
fabs(point.x-prevPoint.x)+2*currentSize,\
fabs(point.y-prevPoint.y)+2*currentSize\
);
[self setNeedsDisplayInRect:rectToRedraw];
}
}

}

// Send over new trace when the touch ends
- (void) touchesEnded:(NSSet *) touches withEvent:(UIEvent *) event
{
[self.arrayAbandonedStrokes removeAllObjects];
}

// Draw all points, foreign and domestic, to the screen
- (void) drawRect: (CGRect) rect
{
//int width = self.pickedImage.size.width;
//int height = self.pickedImage.size.height-44;
//CGRect rectForImage = CGRectMake(512-width/2, 384-height/2, width, height);
//[self.pickedImage drawInRect:rectForImage];

if (self.arrayStrokes)
{
int arraynum = 0;
// each iteration draw a stroke
// line segments within a single stroke (path) has the same color and line width
for (NSDictionary *dictStroke in self.arrayStrokes)
{
NSArray *arrayPointsInstroke = [dictStroke objectForKey:@"points"];
UIColor *color = [dictStroke objectForKey:@"color"];
float size = [[dictStroke objectForKey:@"size"] floatValue];
[color set]; // equivalent to both setFill and setStroke
// draw the stroke, line by line, with rounded joints
UIBezierPath* pathLines = [UIBezierPath bezierPath];
CGPoint pointStart = CGPointFromString([arrayPointsInstroke objectAtIndex:0]);
[pathLines moveToPoint:pointStart];
for (int i = 0; i < (arrayPointsInstroke.count - 1); i++)
{
CGPoint pointNext = CGPointFromString([arrayPointsInstroke objectAtIndex:i+1]);
[pathLines addLineToPoint:pointNext];
}
pathLines.lineWidth = size;
pathLines.lineJoinStyle = kCGLineJoinRound;
pathLines.lineCapStyle = kCGLineCapRound;
[pathLines stroke];

arraynum++;
}
}
}

最佳答案

看看平滑绘图,您可以在这里找到它:

https://github.com/krzysztofzablocki/smooth-drawing

通过使用 UIPanGestureRecognizervelocityInView 方法,并将其值存储在数组中以供绘图时使用,它完全可以满足您的需求。

关于ios - 当我改变手指速度时如何改变画笔大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13337739/

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