简体   繁体   English

在Swift中,UIBezierPath可以画出一条紧随触摸的线条?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM