{"id":1748,"date":"2023-06-27T14:38:38","date_gmt":"2023-06-27T06:38:38","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=1748"},"modified":"2023-06-27T14:38:38","modified_gmt":"2023-06-27T06:38:38","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8ios%e8%ae%be%e5%a4%87%e4%b8%8a%e6%8d%95%e6%8d%89%e7%94%a8%e6%88%b7%e7%ad%be%e5%90%8d","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/1748\/","title":{"rendered":"\u5982\u4f55\u5728ios\u8bbe\u5907\u4e0a\u6355\u6349\u7528\u6237\u7b7e\u540d?"},"content":{"rendered":"
\u5728iOS\u8bbe\u5907\u4e0a\u6355\u6349\u7528\u6237\u7b7e\u540d\u662f\u4e00\u9879\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u5e94\u7528\u573a\u666f\uff0c\u5982\u7535\u5b50\u7b7e\u540d\u3001\u5408\u540c\u7b7e\u7f72\u7b49\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728iOS\u8bbe\u5907\u4e0a\u6355\u6349\u7528\u6237\u7b7e\u540d\u7684\u539f\u7406\u548c\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n
1. \u539f\u7406<\/p>\n
\u5728iOS\u8bbe\u5907\u4e0a\u6355\u6349\u7528\u6237\u7b7e\u540d\u7684\u539f\u7406\u662f\u5229\u7528\u89e6\u6478\u4e8b\u4ef6\u6765\u83b7\u53d6\u7528\u6237\u5728\u5c4f\u5e55\u4e0a\u7684\u624b\u5199\u8f68\u8ff9\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u8f68\u8ff9\u8f6c\u5316\u4e3a\u4e00\u5f20\u56fe\u7247\uff0c\u7528\u4e8e\u4fdd\u5b58\u6216\u5c55\u793a\u3002<\/p>\n
\u5177\u4f53\u5b9e\u73b0\u7684\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n
1.1 \u521b\u5efa\u4e00\u4e2aUIView\u4f5c\u4e3a\u7b7e\u540d\u533a\u57df\uff0c\u5f53\u7528\u6237\u5728\u8be5\u533a\u57df\u5185\u8fdb\u884c\u624b\u5199\u65f6\uff0c\u89e6\u53d1\u89e6\u6478\u4e8b\u4ef6\u3002<\/p>\n
1.2 \u5728\u89e6\u6478\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u83b7\u53d6\u7528\u6237\u624b\u6307\u5728\u5c4f\u5e55\u4e0a\u7684\u4f4d\u7f6e\uff0c\u5e76\u5c06\u8fd9\u4e9b\u4f4d\u7f6e\u70b9\u8fde\u6210\u4e00\u6761\u7ebf\u6bb5\uff0c\u5f62\u6210\u624b\u5199\u8f68\u8ff9\u3002<\/p>\n
1.3 \u5c06\u624b\u5199\u8f68\u8ff9\u7ed8\u5236\u5230\u4e00\u4e2aUIImage\u5bf9\u8c61\u4e0a\uff0c\u7136\u540e\u5c06\u8be5\u56fe\u7247\u4fdd\u5b58\u6216\u5c55\u793a\u3002<\/p>\n
2. \u8be6\u7ec6\u6b65\u9aa4<\/p>\n
\u4e0b\u9762\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728iOS\u8bbe\u5907\u4e0a\u5b9e\u73b0\u6355\u6349\u7528\u6237\u7b7e\u540d\u7684\u529f\u80fd\uff0c\u5305\u62ec\u521b\u5efa\u7b7e\u540d\u533a\u57df\u3001\u83b7\u53d6\u624b\u5199\u8f68\u8ff9\u3001\u7ed8\u5236\u56fe\u7247\u7b49\u3002<\/p>\n
2.1 \u521b\u5efa\u7b7e\u540d\u533a\u57df<\/p>\n
\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aUIView\u4f5c\u4e3a\u7b7e\u540d\u533a\u57df\uff0c\u7528\u4e8e\u63a5\u6536\u7528\u6237\u624b\u5199\u8f68\u8ff9\u3002\u53ef\u4ee5\u5728Storyboard\u6216\u4ee3\u7801\u4e2d\u521b\u5efa\u8be5\u89c6\u56fe\uff0c\u5e76\u8bbe\u7f6e\u5176\u80cc\u666f\u989c\u8272\u3001\u8fb9\u6846\u7b49\u5c5e\u6027\u3002<\/p>\n
2.2 \u83b7\u53d6\u624b\u5199\u8f68\u8ff9<\/p>\n
\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u5728\u7b7e\u540d\u533a\u57df\u4e2d\u6dfb\u52a0\u89e6\u6478\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\uff0c\u4ee5\u83b7\u53d6\u7528\u6237\u624b\u5199\u8f68\u8ff9\u3002<\/p>\n
\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u89e6\u6478\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\uff1a<\/p>\n
“`<\/p>\n
override func touchesBegan(_ touches: Set, with event: UIEvent?) {<\/p>\n
guard let touch = touches.first else { return }<\/p>\n
lastPoint = touch.location(in: self)<\/p>\n
}<\/p>\n
override func touchesMoved(_ touches: Set, with event: UIEvent?) {<\/p>\n guard let touch = touches.first else { return }<\/p>\n<\/p>\n let currentPoint = touch.location(in: self)<\/p>\n<\/p>\n UIGraphicsBeginImageContext(self.frame.size)<\/p>\n self.image?.draw(in: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))<\/p>\n let context = UIGraphicsGetCurrentContext()<\/p>\n context?.setLineWidth(2.0)<\/p>\n context?.setStrokeColor(UIColor.black.cgColor)<\/p>\n context?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))<\/p>\n context?.addLine(to: CGPoint(x: currentPoint.x, y: currentPoint.y))<\/p>\n context?.strokePath()<\/p>\n<\/p>\n self.image = UIGraphicsGetImageFromCurrentImageContext()<\/p>\n UIGraphicsEndImageContext()<\/p>\n<\/p>\n lastPoint = currentPoint<\/p>\n }<\/p>\n override func touchesEnded(_ touches: Set, with event: UIEvent?) {<\/p>\n UIGraphicsBeginImageContext(self.frame.size)<\/p>\n self.image?.draw(in: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))<\/p>\n let context = UIGraphicsGetCurrentContext()<\/p>\n context?.setLineWidth(2.0)<\/p>\n context?.setStrokeColor(UIColor.black.cgColor)<\/p>\n context?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))<\/p>\n context?.addLine(to: CGPoint(x: lastPoint.x, y: lastPoint.y))<\/p>\n context?.strokePath()<\/p>\n<\/p>\n self.image = UIGraphicsGetImageFromCurrentImageContext()<\/p>\n UIGraphicsEndImageContext()<\/p>\n }<\/p>\n “`<\/p>\n \u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0ctouchesBegan\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u7528\u6237\u624b\u6307\u5728\u5c4f\u5e55\u4e0a\u7684\u8d77\u59cb\u4f4d\u7f6e\uff1btouchesMoved\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u7528\u6237\u624b\u6307\u5728\u5c4f\u5e55\u4e0a\u79fb\u52a8\u7684\u4f4d\u7f6e\uff0c\u5e76\u5c06\u8fd9\u4e9b\u4f4d\u7f6e\u70b9\u8fde\u6210\u4e00\u6761\u7ebf\u6bb5\uff1btouchesEnded\u51fd\u6570\u7528\u4e8e\u7ed3\u675f\u624b\u5199\u64cd\u4f5c\u3002<\/p>\n \u5728touchesMoved\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528UIGraphicsBeginImageContext\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u4e0a\u4e0b\u6587\uff0c\u5e76\u5c06\u5f53\u524d\u89c6\u56fe\u7684\u56fe\u82f9\u679c13\u53ef\u4ee5\u8bbe\u7f6e\u7b7e\u540d<\/a>\u50cf\u7ed8\u5236\u5230\u8be5\u4e0a\u4e0b\u6587\u4e2d\u3002\u7136\u540e\uff0c\u4f7f\u7528Core Graphics\u51fd\u6570\u5c06\u624b\u5199\u8f68\u8ff9\u7ed8\u5236\u5230\u56fe\u5f62\u4e0a\u4e0b\u6587\u4e2d\uff0c\u5e76\u5c06\u7ed8\u5236\u597d\u7684\u56fe\u50cf\u4fdd\u5b58\u5230self.image\u5c5e\u6027\u4e2d\u3002\u6700\u540e\uff0c\u4f7f\u7528UIGraphicsEndImageContext\u51fd\u6570\u7ed3\u675f\u56fe\u5f62\u4e0a\u4e0b\u6587\u3002<\/p>\n 2.3 \u7ed8\u5236\u56fe\u7247<\/p>\n \u6700\u540e\uff0c\u9700\u8981\u5c06\u7528\u6237\u624b\u5199\u8f68\u8ff9\u8f6c\u5316\u4e3a\u4e00\u5f20\u56fe\u7247\uff0c\u5e76\u5c06\u8be5\u56fe\u7247\u4fdd\u5b58\u6216\u5c55\u793a\u3002<\/p>\n \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06self.image\u5c5e\u6027\u8f6c\u5316\u4e3a\u4e00\u5f20UIImage\u5bf9\u8c61\uff1a<\/p>\n<\/figure>\n<\/p>\n