ios – 如何使用GraphicContext在Objective C中获得逐渐变细的线条效果

ios – 如何使用GraphicContext在Objective C中获得逐渐变细的线条效果,第1张

概述我正在做一个书法应用程序,并希望修改我的代码,所以当用户结束画笔描边时,线条逐渐变细,并像真正的书法笔一样变薄(轻d效果).我知道touchesEnded可能是一个更好的方法来做到这一点,但是我只是想知道在 Xcode中使用CGRect或GraphicsContext在目标C的Xcode中以编程方式在笔画结束时进行此轻d的最佳方法. - (void)touchesMoved:(NSSet *)to 我正在做一个书法应用程序,并希望修改我的代码,所以当用户结束画笔描边时,线条逐渐变细,并像真正的书法笔一样变薄(轻d效果).我知道touchesEnded可能是一个更好的方法来做到这一点,但是我只是想知道在 Xcode中使用CGRect或GraphicsContext在目标C的Xcode中以编程方式在笔画结束时进行此轻d的最佳方法.

- (voID)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {mouseSwiped = YES;UItouch *touch = [touches anyObject];currentPoint = [touch locationInVIEw:self.vIEw];UIGraphicsBeginImageContext(CGSizeMake(320,568));[drawImage.image drawInRect:CGRectMake(0,320,568)];CGContextSetlineCap(UIGraphicsGetCurrentContext(),kCGlineCapRound);CGContextSetlinewidth(UIGraphicsGetCurrentContext(),5.0);CGContextSetRGBstrokecolor(UIGraphicsGetCurrentContext(),1);CGContextBeginPath(UIGraphicsGetCurrentContext());CGContextMovetoPoint(UIGraphicsGetCurrentContext(),lastPoint.x,lastPoint.y);CGContextAddlinetoPoint(UIGraphicsGetCurrentContext(),currentPoint.x,currentPoint.y);CGContextstrokePath(UIGraphicsGetCurrentContext());[drawImage setFrame:CGRectMake(0,568)];drawImage.image = UIGraphicsGetimageFromCurrentimageContext();UIGraphicsEndImageContext();lastPoint = currentPoint;}- (voID)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {//what code do I put here to get the flick effect - what CGGetContext Parameter//may be applicable or what programming technique may help with this.     }
解决方法 这可能过度简化,但它应该引导你走向正确的方向.我要制作一个三角形,但你最终可以添加贝塞尔曲线以使效果更逼真.

- (voID)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {//what code do I put here to get the flick effect - what CGGetContext Parameter//may be applicable or what programming technique may help with this.     UIGraphicsBeginImageContext(CGSizeMake(320,568));    [drawImage.image drawInRect:CGRectMake(0,568)];    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetlinewidth(context,5.0);    CGContextSetRGBFillcolor(context,1);    CGContextBeginPath(context);    CGContextBeginPath(context);    CGContextMovetoPoint(context,lastPoint.y);    CGContextAddlinetoPoint(context,currentPoint.y);    CGContextClosePath(context);    CGContextstrokePath(context);    //normalized directionality    float lineLength = sqrt((currentPoint.x - lastPoint.x)*(currentPoint.x - lastPoint.x) + (currentPoint.y - lastPoint.y)*(currentPoint.y - lastPoint.y));    float dx = (currentPoint.x - lastPoint.x)/lineLength;    float dy = (currentPoint.y - lastPoint.y)/lineLength;    //Now make a triangle    CGContextBeginPath(context);    //2.5 is from 1/2 of your line wIDth (5)    CGContextMovetoPoint(context,currentPoint.x + 2.5*dy,currentPoint.y - 2.5*dx);    //This 10 is completely arbitrary,the length your taper is going to be.    //IDeally this will be proportional to your last line segment length,longer if their finger is moving faster...    CGContextAddlinetoPoint(context,currentPoint.x + 10*dx,currentPoint.y + 10*dy);    //Now the last tip of the triangle    CGContextMovetoPoint(context,currentPoint.x - 2.5*dy,currentPoint.y + 2.5*dx);    CGContextClosePath(context);    CGContextFillPath(context);    [drawImage setFrame:CGRectMake(0,568)];    drawImage.image = UIGraphicsGetimageFromCurrentimageContext();    UIGraphicsEndImageContext();}

现在为了制作这个散热器,您可以添加人物绘制的曲线计算,并在弯曲的方向上创建具有贝塞尔曲线的“三角形”锥形.这实际上可能非常有趣.

总结

以上是内存溢出为你收集整理的ios – 如何使用GraphicContext在Objective C中获得逐渐变细的线条效果全部内容,希望文章能够帮你解决ios – 如何使用GraphicContext在Objective C中获得逐渐变细的线条效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1038557.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存