{"id":309,"date":"2014-12-29T09:22:52","date_gmt":"2014-12-29T01:22:52","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=309"},"modified":"2016-07-14T22:44:30","modified_gmt":"2016-07-14T14:44:30","slug":"%e4%b8%80%e8%a1%8c%e4%bb%a3%e7%a0%81%e6%90%9e%e5%ae%9aios%e6%89%8b%e5%8a%bf%e8%a7%a3%e9%94%81%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/12\/29\/092252309.html","title":{"rendered":"\u4e00\u884c\u4ee3\u7801\u641e\u5b9aiOS\u624b\u52bf\u89e3\u9501\u529f\u80fd"},"content":{"rendered":"<p><strong>1\u3001\u6709\u56fe\u6709\u771f\u76f8<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-310\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2014\u5e7412\u670827\u65e5-\u4e0b\u53489.46.46.png\" alt=\"iOS Simulator Screen Shot 2014\u5e7412\u670827\u65e5 \u4e0b\u53489.46.46\" width=\"320\" height=\"480\" \/><\/p>\n<p><strong>2\u3001\u8bbe\u8ba1\u601d\u8def<\/strong><\/p>\n<p>2.1\u3001\u4ee5\u5c4f\u5e55\u5bbd\u5ea6\u4e3a\u8fb9\u957f\u521b\u5efa\u4e00\u4e2a\u6b63\u65b9\u5f62View\uff1b<br \/>\n2.2\u3001\u4e00\u4e2afor\u5faa\u73af\u521b\u5efa9\u4e2a\u6309\u94ae\u5e76\u81ea\u52a8\u5e03\u5c40\uff1b<br \/>\n2.3\u3001\u76d1\u542c\u8be5view\u7684\u89e6\u6478\u4e8b\u4ef6\uff0c\u89e6\u6478\u533a\u57df\u843d\u5728\u67d0\u6309\u94ae\u533a\u57df\u5219\u6309\u94ae\u53d1\u5149<br \/>\n2.4\u3001\u8bb0\u5f55\u53d1\u5149\u7684\u6309\u94ae\uff0c\u8f93\u51fa\u6309\u94ae\u987a\u5e8f\uff08\u53ef\u4ee5\u5728for\u5faa\u73af\u521b\u5efa\u6309\u94ae\u65f6\u7ed9\u6309\u94ae\u505atag\u6807\u8bb0\uff0c\u8fd9\u6837\u5c31\u6709\u5bf9\u5e94\u5173\u7cfb\u4e86\uff0c\u4ece\u800c\u628a\u6309\u94ae\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u5bc6\u7801\uff09<!--more--><\/p>\n<p><strong>3\u3001\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<p>SAScreenLockView.h<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/? SAScreenLockView.h\n\/\/? SAScreenLock\n\/\/\n\/\/? Created by \u4f59\u897f\u5b89 on 14\/12\/27.\n\/\/? Copyright (c) 2014\u5e74 Sian. All rights reserved.\n\/\/\n?\n#import &lt;UIKit\/UIKit.h&gt;\n?\n\/***************\u4ee3\u7406\u534f\u8bae***************\/\n?\n@class SAScreenLockView;\n?\n@protocol SAScreenLockViewDelegate \n?\n- (void)screenLock:(SAScreenLockView *)screenLockView didFinishPaht:(NSNumber *)path;\n?\n@end\n?\n\/***************\u5706\u5708\u63a7\u4ef6***************\/\n?\n@interface SACircle : UIButton\n?\n@end\n?\n\/***************\u89e3\u9501\u89c6\u56fe***************\/\n?\n@interface SAScreenLockView : UIView\n?\n@property (nonatomic, weak) id delegate;\n?\n- (id)initWithDelegate:(id)delegate;\n?\n@end<\/pre>\n<p>SAScreenLockView.m<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/? SAScreenLockView.m\n\/\/? SAScreenLock\n\/\/\n\/\/? Created by \u4f59\u897f\u5b89 on 14\/12\/27.\n\/\/? Copyright (c) 2014\u5e74 Sian. All rights reserved.\n\/\/\n?\n#import \"SAScreenLockView.h\"\n?\n\/*********************\u5706\u5708\u63a7\u4ef6*********************\/\n?\n@implementation SACircle\n?\n- (id)initWithCoder:(NSCoder *)aDecoder\n{\n????if (self = [super initWithCoder:aDecoder]) {\n????????[self setup];\n????}\n????return self;\n}\n?\n- (id)initWithFrame:(CGRect)frame\n{\n????self = [super initWithFrame:frame];\n????if (self) {\n????????[self setup];\n????}\n????return self;\n}\n?\n- (void)setup\n{\n????self.userInteractionEnabled = NO;\n????UIImage *normal = [UIImage imageNamed:@\"gesture_node_normal.png\"];\n????UIImage *selected = [UIImage imageNamed:@\"gesture_node_highlighted\"];\n????[self setImage:normal forState:UIControlStateNormal];\n????[self setImage:selected forState:UIControlStateSelected];\n}\n@end\n?\n\/*********************\u89e3\u9501\u89c6\u56fe*********************\/\n?\n@interface SAScreenLockView ()\n?\n@property (nonatomic, strong) NSMutableArray *circles;\n?\n@property (nonatomic, assign) CGPoint??????? position;\n?\n@end\n?\n@implementation SAScreenLockView\n?\n#pragma mark - \u521d\u59cb\u5316\u65b9\u6cd5\n- (id)initWithDelegate:(id)delegate\n{\n????if (self = [super init]) {\n????????self.delegate = delegate;\n????}\n????return self;\n}\n\/\/ xib\u521b\u5efa\u65f6\u8c03\u7528\n- (id)initWithCoder:(NSCoder *)aDecoder\n{\n????if (self = [super initWithCoder:aDecoder]) {\n????????[self setup];\n????}\n????return self;\n}\n\/\/ \u4ee3\u7801\u521b\u5efa\u65f6\u8c03\u7528\n- (id)initWithFrame:(CGRect)frame\n{\n????self = [super initWithFrame:frame];\n????if (self) {\n????????[self setup];\n????}\n????return self;\n}\n\/\/ \u521d\u59cb\u53169\u4e2a\u5b50\u63a7\u4ef6\n- (void)setup\n{\n????self.backgroundColor = [UIColor clearColor];\n????CGFloat width = [[UIScreen mainScreen] bounds].size.width;\n????self.frame = (CGRect){CGPointZero, width, width};\n????for (int i = 0; i &lt; 9; i++) {\n????????SACircle *circle = [SACircle buttonWithType:UIButtonTypeCustom];\n????????circle.tag = i + 1;\n????????[self addSubview:circle];\n????}\n}\n\/\/ \u88ab\u9009\u4e2d\u7684\u5b50\u63a7\u4ef6\n- (NSMutableArray *)circles\n{\n????if (_circles == nil){\n????????_circles = [NSMutableArray array];\n????}\n????return _circles;\n}\n\/\/ \u5b50\u63a7\u4ef6\u4f4d\u7f6e\u8c03\u6574\n- (void)layoutSubviews\n{\n????NSInteger columns = 3;\n????CGFloat width = self.frame.size.width;\n????CGFloat dimeter = 64.0f;\n????CGFloat margic = (width - dimeter * columns) \/ (columns + 1);\n????for (SACircle *circle in self.subviews) {\n????????CGFloat x = (circle.tag - 1) % columns * (dimeter + margic) + margic;\n????????CGFloat y = (circle.tag - 1) \/ columns * (dimeter + margic);\n????????CGFloat w = dimeter;\n????????CGFloat h = dimeter;\n????????circle.frame = (CGRect){x, y, w, h};\n????}\n????CGRect rect = self.frame;\n????rect.size.height = dimeter * 3 + margic * 2;\n????self.frame = rect;\n}\n?\n#pragma mark - \u89e6\u6478\u4ea4\u4e92\u4e8b\u4ef6\u5904\u7406\n- (CGPoint)pointWithTouch:(NSSet *)touches\n{\n????UITouch *touch = [touches anyObject];\n????return [touch locationInView:self];\n}\n?\n- (SACircle *)circleWithTouches:(NSSet *)touches\n{\n????CGPoint pos = [self pointWithTouch:touches];\n????\/\/ \u5224\u65ad\u5f53\u524d\u89e6\u6478\u4f4d\u7f6e\u662f\u5426\u8fdb\u51659\u4e2a\u5706\u5708\u5185\n????for (SACircle *circle in self.subviews) {\n????????CGRect touchArea = circle.frame;\n????????CGFloat w = touchArea.size.width;\n????????touchArea.origin.x += w \/ 4;\n????????touchArea.origin.y += w \/ 4;\n????????touchArea.size.width -= w \/ 2;\n????????touchArea.size.height -= w \/ 2;\n????????if (CGRectContainsPoint(touchArea, pos)){\n????????????return circle;\n????????}\n????}\n????return nil;\n}\n?\n- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event\n{\n????self.position = CGPointZero;\n????SACircle *circle = [self circleWithTouches:touches];\n????\/\/ \u5982\u679c\u8fdb\u5165\u5706\u5708\u5185\u5e76\u4e14\u5f53\u524d\u5706\u5708\u672a\u88ab\u9009\u4e2d\u5219\u52a0\u5165\u5230\u9009\u4e2d\u6570\u7ec4\n????if (circle &amp;&amp; !circle.selected){\n????????circle.selected = YES;\n????????[self.circles addObject:circle];\n????}\n????[self setNeedsDisplay];\n}\n?\n- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event\n{\n????self.position = [self pointWithTouch:touches];\n????SACircle *circle = [self circleWithTouches:touches];\n????\/\/ \u5982\u679c\u8fdb\u5165\u5706\u5708\u5185\u5e76\u4e14\u5f53\u524d\u5706\u5708\u672a\u88ab\u9009\u4e2d\u5219\u52a0\u5165\u5230\u9009\u4e2d\u6570\u7ec4\n????if (circle &amp;&amp; !circle.selected){\n????????circle.selected = YES;\n????????[self.circles addObject:circle];\n????}\n????[self setNeedsDisplay];\n}\n?\n- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event\n{\n????NSMutableString *path = [NSMutableString string];\n????for (SACircle *circle in self.circles) {\n????????circle.selected = NO;\n????????[path appendFormat:@\"%d\", (int)circle.tag];\n????}\n????\/\/ \u5f53\u9009\u4e2d\u7684\u5706\u5708\u4e0d\u4e3a\u7a7a\uff0c\u5e76\u4e14\u4ee3\u7406\u6709\u5b9e\u73b0\u4ee3\u7406\u65b9\u6cd5\u65f6\u901a\u77e5\u4ee3\u7406\n????if ([self.delegate respondsToSelector:@selector(screenLock:didFinishPaht:)] &amp;&amp; self.circles.count)\n????????[self.delegate screenLock:self didFinishPaht:@([path longLongValue])];\n?????\n????[self.circles removeAllObjects];\n????[self setNeedsDisplay];\n}\n?\n- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event\n{\n????[self touchesEnded:touches withEvent:event];\n}\n?\n#pragma mark - \u89c6\u56fe\u7ed8\u5236\u65b9\u6cd5\n- (void)drawRect:(CGRect)rect\n{\n????if (self.circles.count == 0) return;\n????UIBezierPath *path = [UIBezierPath bezierPath];\n????path.lineWidth = 8;\n????path.lineCapStyle = kCGLineCapRound;\n????path.lineJoinStyle = kCGLineJoinRound;\n????for (int i = 0; i &lt; self.circles.count; i++) {\n????????SACircle *circle = self.circles[i];\n????????if (i == 0) {\n????????????[path moveToPoint:circle.center];\n????????} else {\n????????????[path addLineToPoint:circle.center];\n????????}\n????}\n????if (!CGPointEqualToPoint(self.position, CGPointZero))\n????????[path addLineToPoint:self.position];\n????[[UIColor colorWithRed:122\/255.0 green:214\/255.0 blue:250\/255.0 alpha:0.7] set];\n????[path stroke];\n}\n?\n@end<\/pre>\n<p><strong>4\u3001Demo\u4e0b\u8f7d\uff1a<\/strong><a href=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/SAScreenLock.zip\">SAScreenLock<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u6709\u56fe\u6709\u771f\u76f8 2\u3001\u8bbe\u8ba1\u601d\u8def 2.1\u3001\u4ee5\u5c4f\u5e55\u5bbd\u5ea6\u4e3a\u8fb9\u957f\u521b\u5efa\u4e00\u4e2a\u6b63\u65b9\u5f62View\uff1b 2.2\u3001\u4e00\u4e2afor\u5faa\u73af\u521b\u5efa9\u4e2a\u6309\u94ae\u5e76\u81ea\u52a8\u5e03\u5c40\uff1b 2.3\u3001\u76d1\u542c\u8be5view\u7684\u89e6\u6478\u4e8b\u4ef6\uff0c\u89e6\u6478\u533a\u57df\u843d\u5728\u67d0\u6309\u94ae\u533a\u57df\u5219\u6309\u94ae\u53d1\u5149 2.4\u3001\u8bb0\u5f55\u53d1\u5149\u7684\u6309\u94ae\uff0c\u8f93\u51fa\u6309\u94ae\u987a\u5e8f\uff08\u53ef\u4ee5\u5728for\u5faa\u73af\u521b\u5efa\u6309\u94ae\u65f6\u7ed9\u6309\u94ae\u505atag\u6807\u8bb0\uff0c\u8fd9\u6837\u5c31\u6709\u5bf9\u5e94\u5173\u7cfb\u4e86\uff0c\u4ece\u800c\u628a\u6309\u94ae\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u5bc6\u7801\uff09<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[15],"class_list":["post-309","post","type-post","status-publish","format-standard","hentry","category-project","tag-module"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=309"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/309\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}