iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何使用GraphicContext在Objective C中获得逐渐变细的线条效果大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在做一个书法应用程序,并希望修改我的代码,所以当用户结束画笔描边时,线条逐渐变细,并像真正的书法笔一样变薄(轻弹效果).我知道touchesEnded可能是一个更好的方法来做到这一点,但是我只是想知道在 Xcode中使用CGRect或GraphicsContext在目标C的Xcode中以编程方式在笔画结束时进行此轻弹的最佳方法.

- (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中获得逐渐变细的线条效果所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。