通過cgcontextref來畫圖的時候,實現橡皮擦功能的方式有兩種:
1. 將畫筆設置為背景色來實現橡皮擦功能。
這種情況適用於當前設置context的blend mode為normal的時候,即
CGContextSetBlendMode(context, kCGBlendModeNormal);
此時設置背景色的代碼如下:
UIGraphicsBeginImageContext
(
drawBgView
.
bounds
.
size
);
// get the image
[ drawBgView . layer renderInContext: UIGraphicsGetCurrentContext ()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext ();
UIGraphicsEndImageContext ();
self . drawView . currentColor = [ UIColor colorWithPatternImage: image ];
// get the image
[ drawBgView . layer renderInContext: UIGraphicsGetCurrentContext ()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext ();
UIGraphicsEndImageContext ();
self . drawView . currentColor = [ UIColor colorWithPatternImage: image ];
2.通過clearColor來實現橡皮擦功能
個人覺得第二種方式更加簡單方面且適用性更加廣泛。
第一種情況如果背景色可以變化的情況下,則會有問題。第二種方式因此更具有通用性。
CGContextSetBlendMode(context, kCGBlendModeCopy);
此時再設置畫筆顏色為clearColor就可以清除畫筆
WARNING:設置blend mode為copy的情況下,如果畫筆同時支持透明度的調整,則會出現一些小問題,需要注意。
