UIBezierPath在Swift中绘制一条跟随触摸的线?

时间:2014-06-09 01:26:59

标签: ios swift

尝试将this answer转换为Swift,但我不确定如何处理此行:

[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

感谢任何帮助,谢谢!

3 个答案:

答案 0 :(得分:2)

你可以这样做

self.image.drawInRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))

self.tempDrawImg.image .drawInRect(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))

答案 1 :(得分:1)

该答案中的Canvas类是UIImageView的子类,因此该行中的self.image是继承的image属性,类型为UIImage 。在Swift中,转换后的行可能类似于:

self.image.drawInRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))

答案 2 :(得分:1)

你可以这样做

self.image.drawInRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))

self.tempDrawImg.image .drawInRect(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))

以便完整的代码变为

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