前言
在前兩篇給大家介紹各種折線的繪制以及各種時間軸的不同設計,今天繼續給大家分享一篇各種排序的動態實現.好了現在就給大家看看整體的畫面效果.
1)二分法排序進行中顯示的效果
這里寫圖片描述
2)冒泡法排序完成顯示效果
3)插入法排序進行中顯示的效果
看完了這三種不同排序的動態演示后,大家一定非常關心,這些動畫是如何實現的,現在我就來給大家著重介紹一下它的實現.這里我們用到了js與iOS的交互,類似于這種動畫,我們用oc語言來實現就顯得比較困難,用JS來實現它就非常容易.現在我們就給大家介紹介紹這種技術.#實現##1)首先我們來導入一個JS的一個框架jquery.min.js,同時新建一個HTML文件.
2)然后我們來看看HTML文件里面是具體如何實現的.
1.給HTML 文檔定義樣式
<style>*{ margin:0; padding:0px;}body{ background-color:#555;}#box{ width:375px; height:540px; background-color:#000; margin:0 auto; position:relative; overflow:hidden;}#select{ width:375px; height:480px; line-height:00px; text-align:center; margin:00px auto; font-size:18px; color:#fff;}.test{ border:0px solid #CCC; background-color:#fff; position:absolute;bottom:0;}.pass{ background-color:#F00;}.change{ background-color:#0F3;}.changeEnd{ background-color:#FF0;}</style>
2.用select 創建單選或多選菜單。
<div id="box"></div> <div id="select"> 算法:<select id="algorithm"> <option value="1" selected="selected">冒泡算法</option> <option value="2">二分法算法</option> <option value="3">插入算法</option> </select> 元素個數:<select id="num"> <option value="50" selected="selected" >50</option> <option value="40" >40</option> <option value="30" >30</option> <option value="20" >20</option> </select> 執行速度:<select id="speed"> <option value="1" selected="selected" >fast</option> <option value="5" >normal</option> <option value="10" >slow</option> </select> 附加動畫:<input type="checkbox" id="isAnimat" /> <input type="button" value="開始" /> </div>
3.排序算法 js動畫演示運算過程.
4.選擇要執行的動畫
function selectAnimate(arr, selects) { if (selects == 1) { bubbleSort(arr); } if (selects == 2) { cocktailSort(arr); } if (selects == 3) { insertSort(arr); }}```
###5.生成count個 范圍在maxs-mins之間不重復的隨機數
```function getRandom(mins, maxs, count) { if (maxs - mins < count - 1) { return false; } var _this = { limit:{ maxs:maxs, mins:mins, count:count }, rondomArr:[] }; _this.randomFunc = function() { return parseInt(Math.random() * (_this.limit.maxs - _this.limit.mins + 1) + _this.limit.mins); }; _this.in_array = function(val) { for (var i = 0; i < _this.rondomArr.length && _this.rondomArr[i] != val; i++) ; return !(i == _this.rondomArr.length); }; _this.getRandomArr = function() { for (var i = 0; i < _this.limit.count; i++) { var val = _this.randomFunc(); if (_this.in_array(val)) { i--; } else { _this.rondomArr.push(val); } } return _this.rondomArr; }; return _this.getRandomArr();}
6.冒泡算法動畫;
function bubbleSort(arr) { var i = arr.length, j; var tempExchangVal; var timeDo = function() { var time1 = window.setTimeout(function() { if (i > 0) { j = 0; var time2 = window.setInterval(function() { if (j < i - 1) { changeBox(j, "pass"); if (arr[j] > arr[j + 1]) { tempExchangVal = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tempExchangVal; //演示用容器; changeBox(j, "changeEnd", arr[j]); changeBox(j + 1, "change", tempExchangVal); } j++; } else { window.clearInterval(time2); removeBoxColor(); i--; timeDo(); } }, $speed); } else { //結束; doEnd(arr); } }, $speed); }; timeDo();}
7.二分法算法動畫
function cocktailSort(arr) { var i = 0, j; var timedo = function() { var time = window.setTimeout(function() { if (i < arr.length / 2) { j = i; var time2 = window.setInterval(function() { if (j >= arr.length - i - 1) { window.clearInterval(time2); var k = arr.length - i; var time3 = window.setInterval(function() { if (k <= i) { removeBoxColor(); window.clearInterval(time3); timedo(); } changeBox(k, "pass"); if (arr[k] > arr[k + 1]) { var temp = arr[k]; arr[k] = arr[k + 1]; arr[k + 1] = temp; changeBox(k, "changeEnd", arr[k]); changeBox(k + 1, "change", temp); } k--; }, $speed); } changeBox(j, "pass"); if (arr[j] < arr[j - 1]) { var temp = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = temp; changeBox(j - 1, "changeEnd", temp); changeBox(j, "change", arr[j]); } j++; }, $speed); } else { doEnd(arr); } i++; }, $speed); }; timedo();}
8.插入算法
//插入算法function insertSort(arr) {//插入算法; var i = 1; var timedo = function() { var time = window.setTimeout(function() { if (i < arr.length) { var tmp = arr[i]; var key = i - 1; removeBoxColor(); var time2 = window.setInterval(function(){ changeBox(i, "pass"); if(key >= 0 && tmp < arr[key]){ arr[key + 1] = arr[key]; changeBox(key + 1, "change", arr[key]); key--; }else{ if (key + 1 != i) { arr[key + 1] = tmp; changeBox(key + 1, "changeEnd", tmp); } window.clearInterval(time2); timedo(); } },$speed); }else{ doEnd(arr); } i++; }, $speed); } timedo(); }
9.改變容器顏色及其高度;
//改變容器顏色及其高度;
function changeBox(index, className, height) { if (arguments[2]) { if($isAnimat){ var $thisSpeed = 10*$speed; $(".test").eq(index).animate({height:height},$thisSpeed).addClass(className); }else{ $(".test").eq(index).height(height).addClass(className); } } else { $(".test").eq(index).removeClass("change changeEnd").addClass(className); }}
10.清除容器顏色和結束動畫
//清除容器顏色function removeBoxColor() { $(".test").removeClass("pass change changeEnd");}//結束動畫function doEnd(arr) { removeBoxColor(); var i = 0; var time = window.setInterval(function() { if (i >= arr.length) { window.clearInterval(time); $("#select").slideDown(); } $(".test").eq(i).addClass("change").next().addClass("pass"); i++; }, 5);}```
好了,這樣就差不多寫完了,現在我們來具體測試一下HTML與iOS具體交互.
##3)HTML與iOS交互.
###1.首先我們導入一個庫

###2.在控制器里面去實現
```#import "ViewController.h"http://導入相關頭文件#import <JavaScriptCore/JavaScriptCore.h>//遵守協議@interface ViewController ()<UIWebViewDelegate>//storyboard中我們拖一個UIWebView,并且把其拖成一個屬性@property (weak, nonatomic) IBOutlet UIWebView *webView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; _webView.delegate = self;//設置代理 [self.view addSubview:_webView];//加載webviews //WebView和JS交互 NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];//查找html文件 NSURLRequest *request = [NSURLRequest requestWithURL:url]; [_webView loadRequest:request];//加載請求}```
這樣整個過程就弄完了,運行我們看一下效果.
##4)效果



剩下的還有很多效果,大家有興趣的可以自己把源碼下來自己去測試.#結論源碼下載:http://download.csdn.net/detail/baihuaxiu123/9504875
文章地址:http://blog.csdn.net/baihuaxiu123/article/details/51265501