{"id":755,"date":"2017-07-28T18:12:17","date_gmt":"2017-07-28T10:12:17","guid":{"rendered":"http:\/\/www.olinone.com\/?p=755"},"modified":"2023-04-21T00:26:05","modified_gmt":"2023-04-20T16:26:05","slug":"ios%e5%bc%b9%e5%b9%95%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88-hjdanmaku-2-0%e5%8f%91%e5%b8%83","status":"publish","type":"post","link":"http:\/\/www.olinone.com\/?p=755","title":{"rendered":"iOS\u5f39\u5e55\u89e3\u51b3\u65b9\u6848\u2014\u2014HJDanmaku 2.0\u53d1\u5e03"},"content":{"rendered":"<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1a<a title=\"http:\/\/www.olinone.com\/\" href=\"http:\/\/www.olinone.com\/\">http:\/\/www.olinone.com\/<\/a><\/p>\n<p>Hi\uff0c\u597d\u4e45\u4e0d\u89c1\uff0c<a href=\"http:\/\/www.olinone.com\/?p=186\">HJDanmaku 1.0<\/a>\u7248\u672c\u53d1\u5e03\u5df2\u7ecf\u8fc7\u53bb\u4e24\u5e74\u4e4b\u4e45\uff0c\u76f4\u64ad\u884c\u4e1a\u7684\u5feb\u901f\u5d1b\u8d77\u50ac\u751f\u4e86\u76f4\u64ad\u5f39\u5e55\u7684\u8feb\u5207\u9700\u6c42\uff0c\u9ad8\u5e76\u53d1\u3001\u5927\u6d41\u91cf\u3001\u5b9e\u65f6\u6027\u7684\u7279\u6027\u548c\u4ee5\u5f80\u89c6\u9891\u5f39\u5e55\u7684\u573a\u666f\u90fd\u5927\u6709\u4e0d\u540c\uff0c\u4e3a\u4e86\u6ee1\u8db3\u65b0\u7684\u76f4\u64ad\u4e1a\u52a1\u573a\u666f\uff0c<a href=\"https:\/\/github.com\/panghaijiao\/HJDanmakuDemo\">HJDanmaku2.0<\/a>\u6b63\u5f0f\u53d1\u5e03\uff01<\/p>\n<h2>\u6d41\u7545\u5ea6<\/h2>\n<p>\u76f8\u8f83\u4e8e1.0\u7248\u672c\uff0c <a href=\"https:\/\/github.com\/panghaijiao\/HJDanmakuDemo\">HJDanmaku2.0<\/a>\u91c7\u7528\u5168\u65b0\u7684\u5f02\u6b65\u6e32\u67d3\u5f15\u64ce\uff0c98%\u7684\u8ba1\u7b97\u5de5\u4f5c\u8f6c\u79fb\u5230\u5b50\u7ebf\u7a0b\u6267\u884c\uff0c\u907f\u514d\u4e86\u4e3b\u7ebf\u7a0b\u7684\u5361\u987f\u5ef6\u65f6\u3002\u540c\u65f6\uff0c\u53c2\u8003\u79bb\u5c4f\u6e32\u67d3\u6280\u672f\uff0c\u5c06\u7ec4\u88c5\u5f39\u5e55\u548c\u6e32\u67d3\u5f39\u5e55\u5206\u5e03\u5728\u4e24\u4e2a\u72ec\u7acb\u7ebf\u7a0b\u5f02\u6b65\u6267\u884c\uff0c\u786e\u4fdd\u4e86\u5f39\u5e55\u6e32\u67d3\u7684\u6d41\u7545\u6027<\/p>\n<pre class=\"lang:objc highlight:0 decode:true\">NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{\n    NSArray &lt;HJDanmakuAgent *&gt; *danmakuAgents = [self.danmakuSource fetchDanmakuAgentsForTime:(HJDanmakuTime){HJMaxTime(time), time.interval}];\n    dispatch_async(_renderQueue, ^{\n        if (danmakuAgents.count &gt; 0) {\n            [self.danmakuQueuePool insertObjects:danmakuAgents atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, danmakuAgents.count)]];\n        }\n    });\n}];\n[self.sourceQueue cancelAllOperations];\n[self.sourceQueue addOperation:operation];<\/pre>\n<p>\u5c06\u7ec4\u88c5\u5f39\u5e55\u7684\u8fc7\u7a0b\u62c6\u5206\u4e3a\u72ec\u7acb\u7684\u5b50\u7ebf\u7a0b\u4efb\u52a1\uff0c\u7edf\u4e00\u7531NSOperationQueue\u5355\u6267\u884c\u961f\u5217\u7ba1\u7406\uff0c\u6709\u6548\u7684\u964d\u4f4eCPU\u7684\u4f7f\u7528\u7387\uff0c\u63d0\u5347\u7cfb\u7edf\u8fd0\u884c\u7a33\u5b9a\u6027\u3002\u6b64\u5916\uff0c\u57282.0\u7248\u672c\u4e2d\uff0c\u4f7f\u7528CADisplayLink\u66ff\u6362\u5b9a\u65f6\u5668NSTimer\uff0c\u4e0e\u5c4f\u5e55\u5237\u65b0\u9891\u7387\u4fdd\u6301\u4e00\u81f4\uff0c\u53ef\u4ee5\u907f\u514dNSTimer\u7531\u4e8e\u7ebf\u7a0b\u963b\u585e\u5bfc\u81f4\u7684\u5237\u65b0\u5ef6\u65f6<\/p>\n<h2>\u9ad8\u5e76\u53d1<\/h2>\n<p>\u76f4\u64ad\u4e0e\u4f20\u7edf\u89c6\u9891\u6700\u5927\u533a\u522b\u5728\u4e8e\u5176\u5b9e\u65f6\u6027\uff0c\u77ed\u65f6\u95f4\u5927\u91cf\u7684\u5f39\u5e55\u53d1\u9001\u5bf9\u5e95\u5c42\u6e32\u67d3\u5f15\u64ce\u662f\u4e2a\u4e0d\u5c0f\u7684\u6311\u6218\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c<a href=\"https:\/\/github.com\/panghaijiao\/HJDanmakuDemo\">HJDanmaku2.0<\/a>\u5f15\u5165\u6570\u636e\u6e90Source\u7684\u601d\u60f3\uff0c\u5c06\u5f39\u5e55\u63a5\u6536\u4e0e\u7ec4\u88c5\u7684\u8fc7\u7a0b\u5206\u5f00\uff0c\u53ef\u4ee5\u9488\u5bf9\u76f4\u64ad\u3001\u89c6\u9891\u573a\u666f\u5b9e\u73b0\u5dee\u5f02\u5316\u7684\u5904\u7406\u65b9\u6848\u3002\u89c6\u9891\u573a\u666f\u5bf9\u65f6\u95f4\u7cbe\u786e\u5ea6\u8981\u6c42\u8f83\u9ad8\uff0c\u6d89\u53ca\u5230\u5f39\u5e55\u7684\u65f6\u95f4\u6392\u5e8f\uff0c\u540c\u65f6\uff0c\u64ad\u653e\u8fdb\u5ea6\u56de\u653e\u4e5f\u9700\u8981\u6570\u636e\u6e90\u4fdd\u5b58\u6240\u6709\u7684\u5f39\u5e55\u6570\u636e\u3002\u76f4\u64ad\u573a\u666f\u5219\u6bd4\u8f83\u5355\u4e00\uff0c\u64ad\u653e\u5b8c\u53ef\u4ee5\u7acb\u523b\u91ca\u653e\uff0c\u907f\u514d\u5185\u5b58\u7684\u8fc7\u5ea6\u6d88\u8017<\/p>\n<pre class=\"lang:objc highlight:0 decode:true\">u_int interval = 100;\nNSMutableArray *danmakuAgents = [NSMutableArray arrayWithCapacity:interval];\nNSUInteger lastIndex = danmakus.count - 1;\n[danmakus enumerateObjectsUsingBlock:^(HJDanmakuModel *danmaku, NSUInteger idx, BOOL *stop) {\n    HJDanmakuAgent *agent = [[HJDanmakuAgent alloc] initWithDanmakuModel:danmaku];\n    [danmakuAgents addObject:agent];\n    if (idx == lastIndex || danmakuAgents.count % interval == 0) {\n        OSSpinLockLock(&amp;_spinLock);\n        [self.danmakuAgents addObjectsFromArray:danmakuAgents];\n        OSSpinLockUnlock(&amp;_spinLock);\n        [danmakuAgents removeAllObjects];\n     }\n}];<\/pre>\n<p>\u901a\u8fc7\u62c6\u5206\u5165\u5e93\u6570\u636e\u5206\u5e03\u6dfb\u52a0\u53ef\u4ee5\u907f\u514d\u7ebf\u7a0b\u9501\u7684\u957f\u65f6\u95f4\u5360\u6709\uff0c\u63d0\u5347\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u6d41\u7545\u5ea6<\/p>\n<h2>\u7cbe\u786e\u5ea6<\/h2>\n<p>\u4e0e1.0\u7248\u672c\u4e0d\u540c\uff0c\u65b0\u7248\u672c\u901a\u8fc7toleranceCount\u7ef4\u5ea6\u5224\u65ad\u5f39\u5e55\u662f\u5426\u8fc7\u671f\uff0c\u9ed8\u8ba4\u5141\u8bb8\u6700\u59272\u79d2\u8bef\u5dee\u3002\u5f39\u5e55\u5237\u65b0\u9891\u7387\u4e3a0.5\u79d2\uff0c\u5373\u6bcf\u4e2a\u5f39\u5e55\u6709\u6548\u7b49\u5f85\u6b21\u6570\u4e3a2\/0.5 = 4\u6b21\uff0c\u8d85\u8fc74\u6b21\u6ca1\u6709\u6e32\u67d3\u5c06\u81ea\u52a8\u4e22\u5f03<\/p>\n<pre class=\"lang:objc highlight:0 decode:true \">- (void)removeExpiredDanmakusForTime:(HJDanmakuTime)time {\n    [self.danmakuQueuePool enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(HJDanmakuAgent *danmakuAgent, NSUInteger idx, BOOL *stop) {\n        danmakuAgent.toleranceCount --;\n        if (danmakuAgent.toleranceCount &lt;= 0) {\n            [self.danmakuQueuePool removeObjectAtIndex:idx];\n        }\n    }];\n}<\/pre>\n<p>\u5f39\u5e55\u5197\u4f59\u5ea6\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5f39\u5e55\u663e\u793a\u66f4\u52a0\u5e73\u5747\uff0c\u4f18\u5316\u4e86\u5f39\u5e55\u663e\u793a\u6548\u679c\uff0c\u4f46\u662f\u4f1a\u964d\u4f4e\u5f39\u5e55\u663e\u793a\u7684\u7cbe\u786e\u5ea6\uff0c\u7279\u522b\u5bf9\u4e8e\u89c6\u9891\u573a\u666f\uff0c\u76f8\u5bf9\u4e8e1.0\u7248\u672c\u6709\u6240\u4e0b\u964d\uff0c\u5982\u679c\u4f60\u5bf9\u7cbe\u786e\u5ea6\u8981\u6c42\u8f83\u9ad8\uff0c\u53ef\u4ee5\u964d\u4f4etolerance\u5197\u4f59\u503c<\/p>\n<h2>\u78b0\u649e\u68c0\u6d4b<\/h2>\n<p>\u4e0e1.0\u76f8\u540c\uff0c<a href=\"https:\/\/github.com\/panghaijiao\/HJDanmakuDemo\">HJDanmaku2.0<\/a>\u4ecd\u7136\u4f7f\u7528\u7cfb\u7edf\u52a8\u753b\u7684\u65b9\u5f0f\u63d0\u4f9b\u5f39\u5e55\u52a8\u753b\u652f\u6301\uff0c\u4f46\u662f\u78b0\u649e\u68c0\u6d4b\u65b9\u5f0f\u7565\u6709\u4e0d\u540c<\/p>\n<pre class=\"lang:objc highlight:0 decode:true\">- (BOOL)checkLRIsWillHitWithPreDanmaku:(HJDanmakuAgent *)preDanmakuAgent danmaku:(HJDanmakuAgent *)danmakuAgent {\n    CGFloat width = CGRectGetWidth(self.bounds);\n    CGFloat preDanmakuSpeed = (width + preDanmakuAgent.size.width) \/ self.configuration.duration;\n    if (preDanmakuSpeed * (self.configuration.duration - preDanmakuAgent.remainingTime) &lt; preDanmakuAgent.size.width) {\n        return YES;\n    }\n    CGFloat curDanmakuSpeed = (width + danmakuAgent.size.width) \/ self.configuration.duration;\n    if (curDanmakuSpeed * preDanmakuAgent.remainingTime &gt; width) {\n        return YES;\n    }\n    return NO;\n}<\/pre>\n<p>\u5728<a href=\"https:\/\/github.com\/panghaijiao\/HJDanmakuDemo\">HJDanmaku2.0<\/a>\u4e2d\uff0c\u78b0\u649e\u68c0\u6d4b\u4e0d\u518d\u4ee5\u5f39\u5e55\u65f6\u95f4\u70b9\u4e3a\u53c2\u8003\u7ef4\u5ea6\uff0c\u6e32\u67d3\u7684\u5f39\u5e55\u62e5\u6709\u5269\u4f59\u65f6\u95f4\u5c5e\u6027\uff0c\u901a\u8fc7\u5269\u4f59\u65f6\u95f4\u4e0e\u901f\u5ea6\u7684\u5173\u7cfb\u5373\u53ef\u5224\u65ad\u4e24\u8005\u4e4b\u95f4\u662f\u5426\u78b0\u649e\u3002\u540c\u65f6\uff0c2.0\u7248\u672c\u53ea\u5728\u6dfb\u52a0\u5f39\u5e55\u548c\u6062\u590d\u52a8\u753b\u65f6\u4e3a\u5f39\u5e55\u89c6\u56fe\u6dfb\u52a0\u52a8\u753b\uff0c\u5176\u5b83\u65f6\u95f4\u4e0d\u518d\u6821\u9a8c<\/p>\n<h2>\u624b\u52bf<\/h2>\n<p>\u8fd0\u52a8\u89c6\u56fe\u7cfb\u7edf\u9ed8\u8ba4\u65e0\u6cd5\u54cd\u5e94\u624b\u52bf\u4ea4\u4e92\u4e8b\u4ef6\uff0c\u6574\u4e2a\u70b9\u51fb\u4e8b\u4ef6\u4ea4\u7531\u5168\u5c40\u7edf\u4e00\u5904\u7406\u3002HJDanmakuCell\u5b9a\u4e49\u5c5e\u6027selectionStyle\u63a7\u5236\u5f39\u5e55\u80fd\u5426\u70b9\u51fb\uff0c\u9ed8\u8ba4HJDanmakuCellSelectionStyleNone\uff0c\u5373\u4e0d\u80fd\u70b9\u51fb<\/p>\n<pre class=\"lang:objc highlight:0 decode:true\">- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {\n    self.selectDanmakuAgent = nil;\n    HJDanmakuAgent *danmakuAgent = [self danmakuAgentAtPoint:point];\n    if (danmakuAgent) {\n        if (danmakuAgent.danmakuCell.selectionStyle == HJDanmakuCellSelectionStyleDefault) {\n            self.selectDanmakuAgent = danmakuAgent;\n            return self;\n        }\n        CGPoint cellPoint = [self convertPoint:point toView:danmakuAgent.danmakuCell];\n        return [danmakuAgent.danmakuCell hitTest:cellPoint withEvent:event];\n    }\n    return [super hitTest:point withEvent:event];\n}<\/pre>\n<p>\u89c6\u56fe\u6574\u4f53\u54cd\u5e94\u94fe\u53c2\u8003\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5f53\u6536\u5230\u70b9\u51fb\u4e8b\u60c5\u65f6\uff0c\u4f18\u5148\u5224\u65ad\u5f39\u5e55cell\u662f\u5426\u54cd\u5e94\uff0c\u5982\u679c\u54cd\u5e94\u5219\u4ea4\u7531\u5f39\u5e55cell\u5904\u7406\uff0c\u5426\u5219\u4ea4\u7531\u5168\u5c40\u7edf\u4e00\u5904\u7406<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u65f6\u9694\u4e24\u5e74\uff0c<a href=\"https:\/\/github.com\/panghaijiao\/HJDanmakuDemo\">HJDanmaku2.0<\/a>\u5728\u6027\u80fd\u3001\u5e76\u53d1\u4ee5\u53ca\u5b9a\u5236\u578b\u65b9\u9762\u90fd\u6709\u8f83\u5927\u7684\u63d0\u5347\uff0c\u4ee5iphone6\u8bbe\u5907\u6d4b\u8bd5\u4e3a\u4f8b\uff0cCPU\u6574\u4f53\u4f7f\u7528\u7387\u7a33\u5b9a\u57285%\u5de6\u53f3\uff0c\u5927\u5e76\u53d1100\u6761\/\u79d2\u5f39\u5e55\u7684\u6301\u7eed\u8f93\u5165\uff0cFPS\u53ef\u4ee5\u7ef4\u6301\u572855\u5e27\u4ee5\u4e0a<\/p>\n<p>\u76ee\u524d\u6682\u65f6\u652f\u6301OC\uff0cswift\u7248\u672c\u6b63\u5728\u5f00\u53d1\u4e2d\uff0c\u5982\u679c\u4f60\u6709\u610f\u8d21\u732eswift\u4ee3\u7801\uff0c\u53ef\u4ee5\u4e0e\u6211\u8054\u7cfb~<\/p>\n<p>\u5f53\u7136\uff0c\u5982\u679c\u4f60\u559c\u6b22\uff0c\u53ef\u4ee5\u4e3a\u672c\u9879\u76ee<a href=\"https:\/\/github.com\/panghaijiao\/HJDanmakuDemo\">\u70b9\u70b9\u8d5e<\/a><\/p>\n<hr>\n<p><em>\u5199\u5728\u6587\u540e\uff1a<\/em><\/p>\n<p><em>\u65b0\u5efa\u4e86\u4e00\u4e2aiOS\u5f00\u53d1QQ\u4ea4\u6d41\u7fa4\uff08\u9996\u9875\u53f3\u4e0a\u89d2\u5165\u7fa4\uff09\uff0c\u6b22\u8fce\u5e7f\u5927iOS\u5f00\u53d1\u670b\u53cb\u4e00\u540c\u4ea4\u6d41\u5b66\u4e60\u3002\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5Follow\u672c\u4eba<a href=\"https:\/\/github.com\/panghaijiao\">GitHub<\/a>\uff0c\u6216\u8005\u5173\u6ce8\u6211\u7684<a href=\"http:\/\/weibo.com\/u\/5474633407\">\u65b0\u6d6a\u5fae\u535a<\/a>\uff0c\u611f\u8c22\u4f60\u7684\u6765\u8bbf\uff0c\u4e0b\u671f\u518d\u89c1\uff01<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1ahttp:\/\/www.olinone.com\/ Hi\uff0c\u597d\u4e45\u4e0d\u89c1\uff0cHJDanmaku 1.0\u7248\u672c\u2026 <span class=\"read-more\"><a href=\"http:\/\/www.olinone.com\/?p=755\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/www.olinone.com\/index.php?rest_route=\/wp\/v2\/posts\/755"}],"collection":[{"href":"http:\/\/www.olinone.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.olinone.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.olinone.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.olinone.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=755"}],"version-history":[{"count":14,"href":"http:\/\/www.olinone.com\/index.php?rest_route=\/wp\/v2\/posts\/755\/revisions"}],"predecessor-version":[{"id":831,"href":"http:\/\/www.olinone.com\/index.php?rest_route=\/wp\/v2\/posts\/755\/revisions\/831"}],"wp:attachment":[{"href":"http:\/\/www.olinone.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.olinone.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=755"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.olinone.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}