简体   繁体   中英

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:

[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 . In Swift the converted line could be something like:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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