我有一个绘图应用程序,我想在其中创建一个撤消方法.绘图发生在TouchesMoved:方法中.
我正在尝试创建一个CGContextRef并将其推送到堆栈或将其保存在一个上下文属性中,以后可以恢复,但没有任何运气.任何建议都会很棒.这是我的......
UIImageView *drawingSurface;
CGContextRef undoContext;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[drawingSurface.image drawInRect:CGRectMake(0, 0, drawingSurface.image.size.width, drawingSurface.image.size.height)];
UIGraphicsPushContext(context);
// also tried but cant figure how to restore it
undoContext = context;
UIGraphicsEndImageContext();
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个由我的撤销按钮触发的方法......
- (IBAction)restoreUndoImage {
UIGraphicsBeginImageContext(self.view.frame.size);
UIGraphicsPopContext();
drawingSurface.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我相信我的drawingSurface被指定为nil,因为它只是删除了图像中的所有内容.
我的猜测是我不能用pop这样推.但我似乎无法弄清楚如何只保存上下文然后将其推回到drawingSurface上.Hmmmm.任何帮助都会......嗯......乐于助人.提前致谢 -
并且,仅供参考,这是我正在做的绘制到屏幕,这是很好的工作.这是在我的TouchesMoved中:
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[drawingSurface.image drawInRect:CGRectMake(0, 0, drawingSurface.image.size.width, drawingSurface.image.size.height)];
CGContextSetLineCap(context, kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(context, self.brush.size); // for size
CGContextSetStrokeColorWithColor (context,[currentColor CGColor]);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y); …Run Code Online (Sandbox Code Playgroud) 我在UIWebview(webkit控件)中有一个多列布局,但是我在访问元素的"可视"位置时遇到了问题.
我将tapX移动4096(4 x 1024,其中1024是'列表页'的高度'),并获得正确的'tapped'元素,但我不知道我是在边缘还是在元素的中间.我需要绝对的顶部和左侧位置,因此我可以使用矩形(e.left,e.top,width,height)在元素顶部设置图层效果 - 获得正确的e.left和e.top证明是棘手的.
tapElement.offsetTop 忽略列布局,转换似乎不起作用.
var tapElement = document.elementFromPoint(tapX, tapY);
if (!tapElement)
return;
var realLeft = window.getComputedStyle(tapElement).getPropertyValue("offsetLeft");
var realTop = window.getComputedStyle(tapElement).getPropertyValue("offsetTop");
Run Code Online (Sandbox Code Playgroud)
解:
// Figuring out the real top and left positions is difficult
// under the CSS3 multi column layout... But this works :)
/*
// jQuery method
var realLeft = $(tapElement).offset().left;
var realTop = $(tapElement).offset().top;
*/
// DOM method
var box = tapElement.getBoundingClientRect();
var doc = tapElement.ownerDocument;
var docElem = doc.documentElement;
var body = doc.body;
var …Run Code Online (Sandbox Code Playgroud)