{"id":30127,"date":"2024-11-25T09:04:53","date_gmt":"2024-11-25T01:04:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/30127\/"},"modified":"2024-11-25T09:04:53","modified_gmt":"2024-11-25T01:04:53","slug":"ios%e5%bc%80%e5%8f%91%e7%b1%bb%e4%bc%bc%e6%94%af%e4%bb%98%e5%ae%9d%e5%af%86%e7%a0%81%e8%be%93%e5%85%a5%e6%a1%86%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/30127\/","title":{"rendered":"iOS\u5f00\u53d1\u7c7b\u4f3c\u652f\u4ed8\u5b9d\u5bc6\u7801\u8f93\u5165\u6846\u529f\u80fd"},"content":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86ios\u5b9e\u73b0\u7c7b\u4f3c\u5fae\u4fe1\u548c\u652f\u4ed8\u5b9d\u7684\u5bc6\u7801\u8f93\u5165\u6846\uff0c\u901a\u8fc7uikeyinput\u534f\u8bae\u4e3a\u54cd\u5e94\u8005\u63d0\u4f9b\u7b80\u5355\u7684\u952e\u76d8\u8f93\u5165\u7684\u529f\uff0c\u518d\u901a\u8fc7coregraphics\u7ed8\u5236\u51fa\u5bc6\u7801\u8f93\u5165\u6846\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u4eec\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b<\/p>\n<p>\u76ee\u524d\u5728\u9879\u76ee\u4e2d\u9700\u8981\u5b9e\u73b0\u53d1\u7ea2\u5305\u7684\u529f\u80fd\uff0c\u81ea\u5df1\u5c31\u5199\u4e86\u4e00\u4e2a\u5bc6\u7801\u8f93\u5165\u6846\u7684\uff0c\u4e3b\u8981\u7528\u5230\u4e86UIKeyInput\u534f\u8bae\u548cCoreGraphics\uff0c\u6548\u679c\u7c7b\u4f3c\u5fae\u4fe1\u652f\u4ed8\uff0c\u611f\u89c9\u8fd8\u884c\u5c31\u628a\u6211\u7684\u601d\u8def\u548c\u5236\u4f5c\u8fc7\u7a0b\u5199\u4e0b\u6765\u7ed9\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/001\/505\/c0f6d344fa6381ff5767c25c9f5593cb-0.gif\" class=\"aligncenter\" title=\"iOS\u5f00\u53d1\u7c7b\u4f3c\u652f\u4ed8\u5b9d\u5bc6\u7801\u8f93\u5165\u6846\u529f\u80fd\u63d2\u56fe\" alt=\"iOS\u5f00\u53d1\u7c7b\u4f3c\u652f\u4ed8\u5b9d\u5bc6\u7801\u8f93\u5165\u6846\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<p><strong>\u8ba9\u4f60\u7684\u81ea\u5b9a\u4e49View\u5177\u5907\u8f93\u5165\u7684\u529f\u80fd<\/strong>\uff08UIKeyInput\u534f\u8bae\uff09<\/p>\n<p>\u901a\u8fc7UIKeyInput\u534f\u8bae\u53ef\u4ee5\u4e3a\u54cd\u5e94\u8005\u63d0\u4f9b\u7b80\u5355\u7684\u952e\u76d8\u8f93\u5165\u7684\u529f\u80fd\uff0c\u8ba9\u9700\u8981\u952e\u76d8\u7684responder\u6210\u4e3a\u7b2c\u4e00\u54cd\u5e94\u8005\u5c31\u884c\u4e86\u3002UIKeyInput\u534f\u8bae\u5fc5\u987b\u5b9e\u73b0\u7684\u6709\u4e09\u4e2a\u65b9\u6cd5\uff0c\u5206\u522b\u662f\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<pre>#pragma&nbsp;mark&nbsp;-&nbsp;UIKeyInput\r\n\/**\r\n&nbsp;*&nbsp;\u7528\u4e8e\u663e\u793a\u7684\u6587\u672c\u5bf9\u8c61\u662f\u5426\u6709\u4efb\u4f55\u6587\u672c\r\n&nbsp;*\/\r\n-&nbsp;(BOOL)hasText&nbsp;{\r\n&nbsp;&nbsp;return&nbsp;self.textStore.length&nbsp;&amp;gt;&nbsp;0;\r\n}\r\n\r\n\/**\r\n&nbsp;*&nbsp;\u63d2\u5165\u6587\u672c\r\n&nbsp;*\/\r\n-&nbsp;(void)insertText:(NSString&nbsp;*)text&nbsp;{\r\n&nbsp;&nbsp;if&nbsp;(self.textStore.length&nbsp;&nbsp;0)&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;[self.textStore&nbsp;deleteCharactersInRange:NSMakeRange(self.textStore.length&nbsp;-&nbsp;1,&nbsp;1)];\r\n&nbsp;&nbsp;&nbsp;if&nbsp;([self.delegate&nbsp;respondsToSelector:@selector(passWordDidChange:)])&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self.delegate&nbsp;passWordDidChange:self];\r\n&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;}\r\n&nbsp;&nbsp;[self&nbsp;setNeedsDisplay];\r\n}\r\n\r\n\/**\r\n&nbsp;*&nbsp;\u662f\u5426\u80fd\u6210\u4e3a\u7b2c\u4e00\u54cd\u5e94\u8005\r\n&nbsp;*\/\r\n-&nbsp;(BOOL)canBecomeFirstResponder&nbsp;{\r\n&nbsp;&nbsp;return&nbsp;YES;\r\n}\r\n\r\n\/**\r\n&nbsp;*&nbsp;\u70b9\u51fb\u6210\u4e3a\u7b2c\u4e00\u76f8\u5e94\u8005\r\n&nbsp;*\/\r\n-&nbsp;(void)touchesBegan:(NSSet&lt;uitouch&gt;&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event&nbsp;{\r\n&nbsp;&nbsp;if&nbsp;(![self&nbsp;isFirstResponder])&nbsp;{\r\n&nbsp;&nbsp;&nbsp;&nbsp;[self&nbsp;becomeFirstResponder];\r\n&nbsp;&nbsp;}\r\n}&lt;\/uitouch&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u901a\u8fc7CoreGraphics\u7ed8\u5236\u51fa\u5bc6\u7801\u8f93\u5165\u6846<\/strong><\/p>\n<p>\u5b9e\u73b0\u7684\u601d\u8def\u662f\u901a\u8fc7CoreGraphics\u6846\u67b6\u7ed8\u5236\u51fa\u5bc6\u7801\u8f93\u5165\u6846\u7684\u5916\u6846\u548c\u91cc\u9762\u7684\u5c0f\u9ed1\u70b9\uff0c\u7136\u540e\u901a\u8fc7\u4ece\u952e\u76d8\u4e0a\u83b7\u53d6\u5230\u7684\u5224\u65ad\u8f93\u5165\u7684\u4f4d\u6570\uff0c\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre>\/**\r\n&nbsp;*&nbsp;\u8bbe\u7f6e\u6b63\u65b9\u5f62\u7684\u8fb9\u957f\r\n&nbsp;*\/\r\n-&nbsp;(void)setSquareWidth:(CGFloat)squareWidth&nbsp;{\r\n&nbsp;&nbsp;_squareWidth&nbsp;=&nbsp;squareWidth;\r\n&nbsp;&nbsp;[self&nbsp;setNeedsDisplay];\r\n}\r\n\r\n\/**\r\n&nbsp;*&nbsp;\u8bbe\u7f6e\u952e\u76d8\u7684\u7c7b\u578b\r\n&nbsp;*\/\r\n-&nbsp;(UIKeyboardType)keyboardType&nbsp;{\r\n&nbsp;&nbsp;return&nbsp;UIKeyboardTypeNumberPad;\r\n}\r\n\r\n\/**\r\n&nbsp;*&nbsp;\u8bbe\u7f6e\u5bc6\u7801\u7684\u4f4d\u6570\r\n&nbsp;*\/\r\n-&nbsp;(void)setPassWordNum:(NSUInteger)passWordNum&nbsp;{\r\n&nbsp;&nbsp;_passWordNum&nbsp;=&nbsp;passWordNum;\r\n&nbsp;&nbsp;[self&nbsp;setNeedsDisplay];\r\n}\r\n\r\n\/**\r\n&nbsp;*&nbsp;\u7ed8\u5236\r\n&nbsp;*\/\r\n-&nbsp;(void)drawRect:(CGRect)rect&nbsp;{\r\n&nbsp;&nbsp;CGFloat&nbsp;height&nbsp;=&nbsp;rect.size.height;\r\n&nbsp;&nbsp;CGFloat&nbsp;width&nbsp;=&nbsp;rect.size.width;\r\n&nbsp;&nbsp;CGFloat&nbsp;x&nbsp;=&nbsp;(width&nbsp;-&nbsp;self.squareWidth*self.passWordNum)\/2.0;\r\n&nbsp;&nbsp;CGFloat&nbsp;y&nbsp;=&nbsp;(height&nbsp;-&nbsp;self.squareWidth)\/2.0;\r\n&nbsp;&nbsp;CGContextRef&nbsp;context&nbsp;=&nbsp;UIGraphicsGetCurrentContext();\r\n&nbsp;&nbsp;\/\/\u753b\u5916\u6846\r\n&nbsp;&nbsp;CGContextAddRect(context,&nbsp;CGRectMake(&nbsp;x,&nbsp;y,&nbsp;self.squareWidth*self.passWordNum,&nbsp;self.squareWidth));\r\n&nbsp;&nbsp;CGContextSetLineWidth(context,&nbsp;1);\r\n&nbsp;&nbsp;CGContextSetStrokeColorWithColor(context,&nbsp;self.rectColor.CGColor);\r\n&nbsp;&nbsp;CGContextSetFillColorWithColor(context,&nbsp;[UIColor&nbsp;whiteColor].CGColor);\r\n&nbsp;&nbsp;\/\/\u753b\u7ad6\u6761\r\n&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;&lt;p&gt;\u3010\u76f8\u5173\u63a8\u8350\u3011&lt;br&gt;&lt;\/p&gt;&lt;p&gt;1. &lt;span style=\"color: rgb(255, 0, 0);\"&gt;&lt;strong&gt;\u7279\u522b\u63a8\u8350&lt;\/strong&gt;&lt;\/span&gt;\uff1a&lt;a href=\"http:\/\/www.php.cn\/xiazai\/gongju\/714\" target=\"_self\"&gt;\u201cphp\u7a0b\u5e8f\u5458\u5de5\u5177\u7bb1\u201dV0.1\u7248\u672c\u4e0b\u8f7d&lt;\/a&gt;&lt;\/p&gt;&lt;p&gt;2. &lt;a href=\"http:\/\/www.php.cn\/xiazai\/code\/xiaochengxu\" target=\"_self\"&gt;\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5b8c\u6574\u6e90\u7801\u4e0b\u8f7d&lt;\/a&gt;&lt;\/p&gt;&lt;p&gt;3. &lt;a href=\"http:\/\/www.php.cn\/xiazai\/code\/130\" target=\"_self\"&gt;\u5fae\u4fe1\u5c0f\u7a0b\u5e8fdemo\uff1a\u679c\u5e93\u66f4\u65b0\u7248&lt;\/a&gt;&lt;\/p&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fiOS\u5f00\u53d1\u7c7b\u4f3c\u652f\u4ed8\u5b9d\u5bc6\u7801\u8f93\u5165\u6846\u529f\u80fd\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86ios\u5b9e\u73b0\u7c7b\u4f3c\u5fae\u4fe1\u548c\u652f\u4ed8\u5b9d\u7684\u5bc6\u7801\u8f93\u5165\u6846\uff0c\u901a\u8fc7uikeyinput\u534f\u8bae\u4e3a\u54cd\u5e94\u8005\u63d0\u4f9b\u7b80\u5355\u7684\u952e\u76d8\u8f93\u5165\u7684\u529f\uff0c\u518d\u901a\u8fc7coregraphics\u7ed8\u5236\u51fa\u5bc6\u7801\u8f93\u5165\u6846\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u4eec\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b \u76ee\u524d\u5728\u9879\u76ee\u4e2d\u9700\u8981\u5b9e\u73b0\u53d1\u7ea2\u5305\u7684\u529f\u80fd\uff0c\u81ea\u5df1\u5c31\u5199\u4e86\u4e00\u4e2a\u5bc6\u7801\u8f93\u5165\u6846\u7684\uff0c\u4e3b\u8981\u7528\u5230\u4e86UIKeyInput\u534f\u8bae\u548cCoreGraphics\uff0c\u6548\u679c\u7c7b\u4f3c\u5fae\u4fe1\u652f\u4ed8\uff0c\u611f\u89c9\u8fd8\u884c\u5c31\u628a\u6211\u7684\u601d\u8def\u548c\u5236\u4f5c\u8fc7\u7a0b\u5199\u4e0b\u6765\u7ed9\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\u3002 \u8ba9\u4f60\u7684\u81ea\u5b9a\u4e49View\u5177\u5907\u8f93\u5165\u7684\u529f\u80fd\uff08UIKeyInput\u534f\u8bae\uff09 \u901a\u8fc7UIKeyInput\u534f\u8bae\u53ef\u4ee5\u4e3a\u54cd\u5e94\u8005\u63d0\u4f9b\u7b80\u5355\u7684\u952e\u76d8\u8f93\u5165\u7684\u529f\u80fd\uff0c\u8ba9\u9700\u8981\u952e\u76d8\u7684responder\u6210\u4e3a\u7b2c\u4e00\u54cd\u5e94\u8005\u5c31\u884c\u4e86\u3002UIKeyInput\u534f\u8bae\u5fc5\u987b\u5b9e\u73b0\u7684\u6709\u4e09\u4e2a\u65b9\u6cd5\uff0c\u5206\u522b\u662f\u4ee5\u4e0b\u65b9\u6cd5\uff1a #pragma&nbsp;mark&nbsp;&#8211;&nbsp;UIKeyInput \/** &nbsp;*&nbsp;\u7528\u4e8e\u663e\u793a\u7684\u6587\u672c\u5bf9\u8c61\u662f\u5426\u6709\u4efb\u4f55\u6587\u672c &nbsp;*\/ &#8211;&nbsp;(BOOL)hasText&nbsp;{ &nbsp;&nbsp;return&nbsp;self.textStore.length&nbsp;&amp;gt;&nbsp;0; } \/** &nbsp;*&nbsp;\u63d2\u5165\u6587\u672c &nbsp;*\/ &#8211;&nbsp;(void)insertText:(NSString&nbsp;*)text&nbsp;{ &nbsp;&nbsp;if&nbsp;(self.textStore.length&nbsp;&nbsp;0)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;[self.textStore&nbsp;deleteCharactersInRange:NSMakeRange(self.textStore.length&nbsp;&#8211;&nbsp;1,&nbsp;1)]; &nbsp;&nbsp;&nbsp;if&nbsp;([self.delegate&nbsp;respondsToSelector:@selector(passWordDidChange:)])&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self.delegate&nbsp;passWordDidChange:self]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} &nbsp;&nbsp;[self&nbsp;setNeedsDisplay]; } \/** &nbsp;*&nbsp;\u662f\u5426\u80fd\u6210\u4e3a\u7b2c\u4e00\u54cd\u5e94\u8005 &nbsp;*\/ &#8211;&nbsp;(BOOL)canBecomeFirstResponder&nbsp;{ &nbsp;&nbsp;return&nbsp;YES; } \/** &nbsp;*&nbsp;\u70b9\u51fb\u6210\u4e3a\u7b2c\u4e00\u76f8\u5e94\u8005 &nbsp;*\/ &#8211;&nbsp;(void)touchesBegan:(NSSet&lt;uitouch&gt;&nbsp;*)touches&nbsp;withEvent:(UIEvent&nbsp;*)event&nbsp;{ &nbsp;&nbsp;if&nbsp;(![self&nbsp;isFirstResponder])&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;[self&nbsp;becomeFirstResponder]; &nbsp;&nbsp;} }&lt;\/uitouch&gt; \u767b\u5f55\u540e\u590d\u5236 \u901a\u8fc7CoreGraphics\u7ed8\u5236\u51fa\u5bc6\u7801\u8f93\u5165\u6846 \u5b9e\u73b0\u7684\u601d\u8def\u662f\u901a\u8fc7CoreGraphics\u6846\u67b6\u7ed8\u5236\u51fa\u5bc6\u7801\u8f93\u5165\u6846\u7684\u5916\u6846\u548c\u91cc\u9762\u7684\u5c0f\u9ed1\u70b9\uff0c\u7136\u540e\u901a\u8fc7\u4ece\u952e\u76d8\u4e0a\u83b7\u53d6\u5230\u7684\u5224\u65ad\u8f93\u5165\u7684\u4f4d\u6570\uff0c\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a \/** &nbsp;*&nbsp;\u8bbe\u7f6e\u6b63\u65b9\u5f62\u7684\u8fb9\u957f &nbsp;*\/ &#8211;&nbsp;(void)setSquareWidth:(CGFloat)squareWidth&nbsp;{ &nbsp;&nbsp;_squareWidth&nbsp;=&nbsp;squareWidth; &nbsp;&nbsp;[self&nbsp;setNeedsDisplay]; } \/** &nbsp;*&nbsp;\u8bbe\u7f6e\u952e\u76d8\u7684\u7c7b\u578b &nbsp;*\/ &#8211;&nbsp;(UIKeyboardType)keyboardType&nbsp;{ &nbsp;&nbsp;return&nbsp;UIKeyboardTypeNumberPad; } \/** &nbsp;*&nbsp;\u8bbe\u7f6e\u5bc6\u7801\u7684\u4f4d\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-30127","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=30127"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30127\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=30127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=30127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=30127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}