[英]UIBezierPath to draw a line that follows a touch, in Swift?
Trying to convert this answer to Swift, but I'm not sure what to do about this line: 尝试将此答案转换为Swift,但是我不确定该行怎么做:
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
Any help is appreciated, thanks! 任何帮助表示赞赏,谢谢!
You can do this like 你可以这样做
self.image.drawInRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
or 要么
self.tempDrawImg.image .drawInRect(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
The Canvas
class in that answer is a subclass of UIImageView
, so the self.image
in that line is the inherited image
property, of type UIImage
. 该答案中的
Canvas
类是UIImageView
的子类,因此该self.image
中的self.image
是继承的image
属性,类型为UIImage
。 In Swift the converted line could be something like: 在Swift中,转换后的行可能类似于:
self.image.drawInRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
You can do this like 你可以这样做
self.image.drawInRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
or 要么
self.tempDrawImg.image .drawInRect(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
so that the full code becomes 这样完整的代码就变成了
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
self.tempDrawImg.image .drawInRect(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, self.location.x, self.location.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
self.image = UIGraphicsGetImageFromCurrentImageContext();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.