{"id":272,"date":"2014-12-19T17:40:57","date_gmt":"2014-12-19T09:40:57","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=272"},"modified":"2016-07-14T22:48:33","modified_gmt":"2016-07-14T14:48:33","slug":"%e7%9b%91%e5%90%ac%e5%b1%8f%e5%b9%95%e8%a7%a6%e6%91%b8%e4%ba%8b%e4%bb%b6%e5%ae%9e%e7%8e%b0%e6%b6%82%e9%b8%a6","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/12\/19\/174057272.html","title":{"rendered":"\u76d1\u542c\u5c4f\u5e55\u89e6\u6478\u4e8b\u4ef6\u5b9e\u73b0\u6d82\u9e26"},"content":{"rendered":"<p><strong>\u4e00\u3001\u76f4\u63a5\u4e0a\u56fe<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-273\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2014\u5e7412\u670819\u65e5-\u4e0b\u53485.09.07.png\" alt=\"iOS-Simulator-Screen-Shot-2014\u5e7412\u670819\u65e5-\u4e0b\u53485.09.07\" width=\"200\" height=\"300\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-274\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2014\u5e7412\u670819\u65e5-\u4e0b\u53485.13.07.png\" alt=\"iOS-Simulator-Screen-Shot-2014\u5e7412\u670819\u65e5-\u4e0b\u53485.13.07\" width=\"200\" height=\"300\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-275\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2014\u5e7412\u670819\u65e5-\u4e0b\u53485.15.02.png\" alt=\"iOS-Simulator-Screen-Shot-2014\u5e7412\u670819\u65e5-\u4e0b\u53485.15.02\" width=\"200\" height=\"300\" \/><\/p>\n<p><strong>\u4e8c\u3001\u8bbe\u8ba1\u8bf4\u660e<\/strong><!--more--><\/p>\n<p>1\u3001UIView\u7ee7\u627f\u81eaUIResponder\uff0cUIResponder\u80fd\u54cd\u5e94\u6240\u6709\u7684\u4e8b\u4ef6\u4e0e\u7528\u6237\u8fdb\u884c\u4ea4\u4e92\uff0c\u5982\uff1a\u89e6\u6478\u4e8b\u4ef6(\u5355\u70b9\/\u591a\u70b9\u89e6\u63a7)\u3001\u52a0\u901f\u4e8b\u4ef6(\u91cd\u529b\u611f\u5e94)\u3001\u8fdc\u7a0b\u4e8b\u4ef6(\u8033\u673a\u64ad\u653e\/\u6682\u505c\/\u4e0b\u4e00\u66f2)<\/p>\n<p>2\u3001\u89e6\u6478\u4e8b\u4ef6\u4e3b\u8981\u6d89\u53ca\u4ee5\u4e0b4\u4e2a\u65b9\u6cd5<\/p>\n<p><span style=\"font-family: Arial;\">\/\/ \u5f00\u59cb\u89e6\u5c4f\u65f6\u8c03\u7528\uff0c\u53ea\u8c03\u7528\u4e00\u6b21<br \/>\n<span style=\"color: sienna;\">&#8211; (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;<\/span><\/p>\n<p>\/\/ \u5c4f\u5e55\u4e0a\u79fb\u52a8\u65f6\u8c03\u7528\uff0c\u53cd\u590d\u8c03\u7528\u4e14\u9891\u7387\u8f83\u9ad8<br \/>\n<span style=\"color: sienna;\">&#8211; (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;<\/span><\/p>\n<p>\/\/ \u79bb\u5f00\u5c4f\u5e55\u65f6\u8c03\u7528\uff0c\u53ea\u8c03\u7528\u4e00\u6b21<br \/>\n<span style=\"color: sienna;\">&#8211; (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;<\/span><\/p>\n<p>\/\/ \u4e2d\u65ad\u65f6\u8c03\u7528\uff0c\u5982\u88ab\u6765\u7535\u4e2d\u65ad<br \/>\n<span style=\"color: sienna;\">&#8211; (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;<\/span><\/span><\/p>\n<p>3\u3001\u7ed3\u5408\u4e0a\u8ff0\u76f8\u5173\u65b9\u6cd5\uff0c\u4f7f\u7528Quartz2D\u7ed8\u5236\u56fe\u5f62\u5230\u5f53\u524d\u89c6\u56fe<\/p>\n<p>4\u3001\u5c06\u56fe\u5f62\u4fdd\u5b58\u5230\u672c\u5730\u76f8\u518c<\/p>\n<p><strong>\u4e09\u3001\u5173\u952e\u4ee3\u7801<\/strong><\/p>\n<pre lang=\"objc\" line=\"1\">#import \"SAView.h\"\n\n@implementation SAView\n\n#pragma mark - \u521d\u59cb\u5316\n- (NSMutableArray *)paths\n{\n    if (_paths == nil) {\n        _paths = [NSMutableArray array];\n    }\n    return _paths;\n}\n\/\/ \u56fe\u5c42\u6e32\u67d3\u65b9\u6cd5\uff0c\u6bcf\u8c03\u7528setNeedsDisplay\u65b9\u6cd5\u90fd\u4f1a\u8c03\u7528\u6b64\u65b9\u6cd5\n- (void)drawRect:(CGRect)rect\n{\n    for (UIBezierPath *path in self.paths) {\n        [path stroke];\n    }\n}\n\n#pragma mark - \u89e6\u6478\u4e8b\u4ef6\u5904\u7406\n\/\/ \u5f00\u59cb\u6d82\u9e26\uff08\u4ee5\u6bcf\u4e00\u7b14\u4e3a\u57fa\u672c\u5355\u4f4d\uff09\uff0c\u63a5\u89e6\u5c4f\u5e55\u65f6\u8c03\u7528\u4e00\u6b21\n- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event\n{\n    NSLog(@\"touchesBegan\");\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u8d1d\u8d5b\u5c14\u66f2\u7ebf\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u76f8\u5173\u5c5e\u6027\uff08\u5706\u89d2\u7ebf\u6761\u3001\u7ebf\u6761\u7c97\u7ec6\uff09\n    UIBezierPath *path = [UIBezierPath bezierPath];\n    path.lineCapStyle = kCGLineCapRound;\n    path.lineJoinStyle = kCGLineJoinRound;\n    path.lineWidth = 5;\n    \n    \/\/ \u901a\u8fc7touch\u4e8b\u4ef6\u83b7\u53d6\u4f4d\u7f6e\u53d8\u5316\u4f20\u5165\u7ed9\u8d1d\u8d5b\u5c14\u66f2\u7ebf\n    CGPoint point = [[touches anyObject] locationInView:self];\n    [path moveToPoint:point];\n    \n    \/\/ \u5c06\u8be5\u66f2\u7ebf\u5bf9\u8c61\u52a0\u5165\u5230\u6570\u7ec4\n    [self.paths addObject:path];\n    \n    \/\/ \u5237\u65b0\u56fe\u5c42\n    [self setNeedsDisplay];\n}\n\n\/\/ \u79fb\u52a8\u624b\u6307\uff0c\u624b\u6307\u79fb\u52a8\u65f6\u4f1a\u53cd\u590d\u8c03\u7528\uff0c\u9891\u7387\u8f83\u9ad8\n- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event\n{\n    NSLog(@\"touchesMoved\");\n    \/* \u83b7\u53d6\u5f53\u524d\u753b\u7b14\u8ddf\u968f\u89e6\u6478\u79fb\u52a8\u7ee7\u7eed\u7ed8\u5236\n     * \u8be5\u65b9\u6cd5\u8c03\u7528\u9891\u7387\u975e\u5e38\u9ad8\uff0c\u4e00\u70b9\u70b9\u79fb\u52a8\u90fd\u4f1a\u8c03\u7528\u591a\u6b21\n     * \u6240\u4ee5\u753b\u51fa\u7684\u591a\u6761\u6298\u7ebf\u89c6\u89c9\u4e0a\u662f\u8fde\u7eed\u7684\n     *\/\n    UIBezierPath *path = [self.paths lastObject];\n    CGPoint point = [[touches anyObject] locationInView:self];\n    [path addLineToPoint:point];\n    \n    \/\/ \u5237\u65b0\u56fe\u5c42\n    [self setNeedsDisplay];\n    \n}\n\n\/\/ \u7ed3\u675f\u89e6\u6478\uff0c\u5373\u624b\u6307\u79bb\u5f00\u65f6\u8c03\u7528\u4e00\u6b21\n- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event\n{\n    NSLog(@\"touchesEnded\");\n    [self touchesMoved:touches withEvent:event];\n}\n\n#pragma mark - \u6309\u94ae\u4e8b\u4ef6\u5904\u7406\n\/\/ \u56de\u9000\uff0c\u5373\u79fb\u9664\u4e0a\u4e00\u4e2a\u753b\u7b14\u5bf9\u8c61\n- (IBAction)back:(id)sender\n{\n    [self.paths removeLastObject];\n    [self setNeedsDisplay];\n}\n\/\/ \u6e05\u9664\uff0c\u79fb\u9664\u6240\u6709\u753b\u7b14\u5bf9\u8c61\n- (IBAction)clean:(id)sender\n{\n    [self.paths removeAllObjects];\n    [self setNeedsDisplay];\n}\n\/\/ \u4fdd\u5b58\u56fe\u7247\u5230\u672c\u5730\u76f8\u518c\n- (IBAction)save:(id)sender\n{\n    \/\/ \u5f00\u542f\u56fe\u7247\u4e0a\u4e0b\u6587\n    UIGraphicsBeginImageContext(self.frame.size);\n    \n    \/\/ \u5c06\u5f53\u524dview\u4e0a\u5185\u5b58\u6e32\u67d3\u5230\u5f53\u524d\u4e0a\u4e0b\u6587\n    [self.layer renderInContext:UIGraphicsGetCurrentContext()];\n    \n    \/\/ \u5c06\u5f53\u524d\u4e0a\u4e0b\u6587\u5185\u5bb9\u8f6c\u6362\u4e3a\u56fe\u7247\u5bf9\u8c61\n    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();\n    \n    \/\/ \u5173\u95ed\u56fe\u7247\u4e0a\u4e0b\u6587\n    UIGraphicsEndImageContext();\n    \n    \/\/ \u5c06\u56fe\u7247\u4fdd\u5b58\u5230\u672c\u5730\u76f8\u518c\n    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);\n}\n@end<\/pre>\n<p><strong>\u56db\u3001Demo\u4e0b\u8f7d\uff1a<\/strong><a href=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/UITouch.zip\">UITouch<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u76f4\u63a5\u4e0a\u56fe \u4e8c\u3001\u8bbe\u8ba1\u8bf4\u660e<\/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":[16,15],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-project","tag-quartz2d","tag-module"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/272","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=272"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}