我正在开发一个用于徒手钻的刷子应用程序,我是核心Graphics框架的新手。
现在我的应用程序已准备好仅使用以下代码进行绘制
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!)
{
println("Enter in touchesBegan method");
mouseSwiped = false;
var touch : UITouch = touches.anyObject() as UITouch;
lastPoint = touch.locationInView(self.view);
}
override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!)
{
println("Enter in touchesMoved method");
mouseSwiped = true;
var touch : UITouch = touches.anyObject() as UITouch;
var currentPoint : CGPoint = touch.locationInView(self.view);
UIGraphicsBeginImageContext(self.view.frame.size);
self.tempDrawImage!.image?.drawInRect(CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height));
println("lastPoint.X :\(lastPoint!.x) , lastPoint.Y :\(lastPoint!.y)")
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint!.x, lastPoint!.y);
println("currentPoint.X :\(currentPoint.x) , currentPoint.Y :\(currentPoint.y)")
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red!, green!, blue!, 1.0)
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush!);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
//contexts!.addObject(UIGraphicsGetCurrentContext());
self.tempDrawImage!.image = UIGraphicsGetImageFromCurrentImageContext();
tempDrawImage!.alpha = opacity!;
UIGraphicsEndImageContext();
println("dot");
lastPoint = currentPoint;
}
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!)
{
println("Enter in touchesEnded method");
if mouseSwiped == false
{
println("false");
UIGraphicsBeginImageContext(self.view.frame.size);
self.tempDrawImage!.image?.drawInRect(CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height));
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush!);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red!, green!, blue!, opacity!);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint!.x, lastPoint!.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint!.x, lastPoint!.y);
CGContextStrokePath(UIGraphicsGetCurrentContext())
CGContextFlush(UIGraphicsGetCurrentContext());
self.tempDrawImage!.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
UIGraphicsBeginImageContext(self.mainImage!.frame.size);
self.mainImage!.image?.drawInRect(CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height ), blendMode: kCGBlendModeNormal, alpha: 1.0);
self.tempDrawImage!.image.drawInRect(CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height), blendMode: kCGBlendModeNormal, alpha: opacity!);
self.mainImage!.image = UIGraphicsGetImageFromCurrentImageContext();
self.tempDrawImage!.image = nil
UIGraphicsEndImageContext();
}
但是在某些应用程序中我看到他们在他们的应用程序中提供撤消功能所以我也想给这个设施,如果使用绘制2行并按下撤销按钮而不是最后添加的行从图像视图逐个删除
我搜索了但是我无法理解并且没有一个答案被接受所以我很困惑所以请帮助我。
谢谢!
答案 0 :(得分:0)
在您的情况下,因为您只是添加线条,这变得非常简单。您需要做的就是创建一个可变数组,其中包含用户输入的所有点。
现在,在每次触摸开始或最后将使用的任何内容中,您将从代码中已经获得的点创建NSValue
,并将此值添加到我之前提到的数组中。添加完所有行之后,您需要做的就是调用一些“刷新”方法,或者更确切地说通过使用setNeedsDisplay
来重新显示视图,这将触发调用draw rect方法。其余代码将从此方法中移除......
现在在撤消按钮的目标中,您将从阵列中删除最后2个点(我希望这里没有问题)并调用刷新或重新显示(与触摸事件相同)。
因此,无论是将绘图代码移动到“刷新”方法还是“绘制矩形”,您现在需要做的是首先清除上下文背景,然后遍历您的点阵列并绘制每一行
够简单吗?