版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.08.05 |
前言
iOS中有關(guān)視圖控件用戶能看到的都在UIKit框架里面荧库,用戶交互也是通過UIKit進行的霉咨,這一篇就看一下UIKit動力學(xué)和移動效果。感興趣的參考上面幾篇文章牺陶。
1. UIKit框架(一) —— UIKit動力學(xué)和移動效果(一)
Invisible boundaries and collisions - 不可見的邊界和碰撞
將碰撞行為初始化更改回其原始形式丙唧,也就是只對那個正放心的有作用:
Swift版本
collision = UICollisionBehavior(items: [square])
在此行之后愈魏,立即添加以下內(nèi)容:
// add a boundary that has the same frame as the barrier
collision.addBoundaryWithIdentifier("barrier", forPath: UIBezierPath(rect: barrier.frame))
OC版本
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.dynamicView]];
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.barrierView.frame];
[collision addBoundaryWithIdentifier:@"barrier" forPath:path];
上面的代碼添加了一個不可見的邊界,它與障礙視圖具有相同的frame想际。 紅色屏障對用戶仍然可見培漏,但對動態(tài)引擎不可見,由于邊界對動態(tài)引擎可見胡本,但對用戶不可見牌柄。 隨著正方形視圖的下降,它就與障礙物相互作用侧甫,但它實際上撞擊了不可移動的邊界珊佣。
下面我們重新修改下barrier的約束,如下所示:
運行代碼可見如下效果:
方形視圖現(xiàn)在從邊界反彈披粟,旋轉(zhuǎn)一點咒锻,然后繼續(xù)向屏幕底部運動。
到目前為止守屉,UIKit Dynamics的強大功能正在變得非常明確:只需幾行代碼就可以完成很多工作惑艇。 引擎蓋下發(fā)生了很多事情;下面部分將向您展示動態(tài)引擎如何與應(yīng)用中的對象進行交互的一些細(xì)節(jié)拇泛。
Behind the scenes of collisions - 在碰撞的幕后
每個動態(tài)行為都有一個action
屬性滨巴,您可以在其中提供要在動畫的每個步驟中執(zhí)行的塊。 將以下代碼添加到viewDidLoad:
Swift版本
collision.action = {
println("\(NSStringFromCGAffineTransform(square.transform)) \(NSStringFromCGPoint(square.center))")
}
上面的代碼記錄了下降方塊視圖的中心和變換屬性俺叭。 Build并運行您的應(yīng)用程序兢卵,您將在Xcode控制臺窗口中看到這些日志消息。
對于前?400
毫秒绪颖,您應(yīng)該看到如下所示的日志消息:
[1, 0, 0, 1, 0, 0], {150, 236}
[1, 0, 0, 1, 0, 0], {150, 243}
[1, 0, 0, 1, 0, 0], {150, 250}
在這里,您可以看到動態(tài)引擎正在改變方形的中心 - 即每個動畫步驟中的frame。
一旦方塊撞到屏障柠横,它就會開始旋轉(zhuǎn)窃款,從而產(chǎn)生如下消息:
[0.99797821, 0.063557133, -0.063557133, 0.99797821, 0, 0] {152, 247}
[0.99192101, 0.12685727, -0.12685727, 0.99192101, 0, 0] {154, 244}
[0.97873402, 0.20513339, -0.20513339, 0.97873402, 0, 0] {157, 241}
在這里,您可以看到動態(tài)引擎正在使用變換和幀偏移的組合來根據(jù)基礎(chǔ)物理模型定位視圖牍氛。
OC版本
collision.action = ^{
NSLog(@"%@--%@", NSStringFromCGAffineTransform(self.dynamicView.transform), NSStringFromCGPoint(self.dynamicView.center));
};
下面看一下輸出結(jié)果
2018-08-05 15:09:45.801516+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 120}
2018-08-05 15:09:45.806114+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 121}
2018-08-05 15:09:45.825243+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 122}
2018-08-05 15:09:45.837939+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 123}
2018-08-05 15:09:45.854657+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 125}
2018-08-05 15:09:45.874589+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 126}
2018-08-05 15:09:45.890322+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 128}
2018-08-05 15:09:45.906109+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 131}
2018-08-05 15:09:45.921364+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 133}
2018-08-05 15:09:45.938558+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 136}
2018-08-05 15:09:46.085647+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 168}
2018-08-05 15:09:46.088024+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 174}
2018-08-05 15:09:46.104674+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 179}
2018-08-05 15:09:46.121365+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 185}
2018-08-05 15:09:46.138057+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 191}
2018-08-05 15:09:46.154729+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 197}
2018-08-05 15:09:46.171396+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 204}
2018-08-05 15:09:46.188074+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 211}
2018-08-05 15:09:46.204805+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 218}
2018-08-05 15:09:46.221432+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 225}
2018-08-05 15:09:46.238041+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 233}
2018-08-05 15:09:46.254805+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 241}
2018-08-05 15:09:46.271686+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 249}
2018-08-05 15:09:46.288085+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 258}
2018-08-05 15:09:46.304718+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 266}
2018-08-05 15:09:46.321378+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 275}
2018-08-05 15:09:46.338054+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 284}
2018-08-05 15:09:46.354709+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 294}
2018-08-05 15:09:46.371506+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 304}
2018-08-05 15:09:46.388231+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 314}
2018-08-05 15:09:46.405078+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 324}
2018-08-05 15:09:46.421429+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 335}
2018-08-05 15:09:46.438066+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 345}
2018-08-05 15:09:46.454790+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 356}
2018-08-05 15:09:46.471348+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 368}
2018-08-05 15:09:46.488277+0800 JJDynamics[13854:6043679] [1, 0, 0, 1, 0, 0]--{187, 379}
2018-08-05 15:09:46.505091+0800 JJDynamics[13854:6043679] [0.99999032001561705, 0.0043999858026804103, -0.0043999858026804103, 0.99999032001561705, 0, 0]--{187, 391}
2018-08-05 15:09:46.521487+0800 JJDynamics[13854:6043679] [0.99670509099572935, 0.081110798191084202, -0.081110798191084202, 0.99670509099572935, 0, 0]--{190, 390}
2018-08-05 15:09:46.538197+0800 JJDynamics[13854:6043679] [0.98757539405837946, 0.15714592279291401, -0.15714592279291401, 0.98757539405837946, 0, 0]--{192, 390}
2018-08-05 15:09:46.554757+0800 JJDynamics[13854:6043679] [0.97270030449661105, 0.23206489961258717, -0.23206489961258717, 0.97270030449661105, 0, 0]--{195, 389}
2018-08-05 15:09:46.571451+0800 JJDynamics[13854:6043679] [0.95215036329479197, 0.30562998164053801, -0.30562998164053801, 0.95215036329479197, 0, 0]--{197, 390}
2018-08-05 15:09:46.588097+0800 JJDynamics[13854:6043679] [0.92604546130418763, 0.37741198125909348, -0.37741198125909348, 0.92604546130418763, 0, 0]--{200, 390}
2018-08-05 15:09:46.604798+0800 JJDynamics[13854:6043679] [0.89471662296917953, 0.44663426266087891, -0.44663426266087891, 0.89471662296917953, 0, 0]--{202, 391}
2018-08-05 15:09:46.621871+0800 JJDynamics[13854:6043679] [0.85822238841548293, 0.51327802604672634, -0.51327802604672634, 0.85822238841548293, 0, 0]--{205, 392}
2018-08-05 15:09:46.638148+0800 JJDynamics[13854:6043679] [0.81677344690082287, 0.57695852228539679, -0.57695852228539679, 0.81677344690082287, 0, 0]--{207, 393}
2018-08-05 15:09:46.654829+0800 JJDynamics[13854:6043679] [0.77073653850693524, 0.63715397527626516, -0.63715397527626516, 0.77073653850693524, 0, 0]--{210, 395}
2018-08-05 15:09:46.671455+0800 JJDynamics[13854:6043679] [0.72041209900074288, 0.69354625484775256, -0.69354625484775256, 0.72041209900074288, 0, 0]--{212, 397}
2018-08-05 15:09:46.688130+0800 JJDynamics[13854:6043679] [0.66612138765966766, 0.74584334608707126, -0.74584334608707126, 0.66612138765966766, 0, 0]--{215, 399}
2018-08-05 15:09:46.704924+0800 JJDynamics[13854:6043679] [0.60804546345526223, 0.79390220705794445, -0.79390220705794445, 0.60804546345526223, 0, 0]--{217, 401}
2018-08-05 15:09:46.721482+0800 JJDynamics[13854:6043679] [0.54668181993310971, 0.83734042524807251, -0.83734042524807251, 0.54668181993310971, 0, 0]--{220, 404}
2018-08-05 15:09:46.738822+0800 JJDynamics[13854:6043679] [0.48222812546060806, 0.87604568089497936, -0.87604568089497936, 0.48222812546060806, 0, 0]--{222, 407}
2018-08-05 15:09:46.754871+0800 JJDynamics[13854:6043679] [0.41523064806151799, 0.90971616942341516, -0.90971616942341516, 0.41523064806151799, 0, 0]--{225, 410}
2018-08-05 15:09:46.771468+0800 JJDynamics[13854:6043679] [0.34608624036957419, 0.93820270423126217, -0.93820270423126217, 0.34608624036957419, 0, 0]--{227, 413}
2018-08-05 15:09:46.788139+0800 JJDynamics[13854:6043679] [0.27500636898821867, 0.96144240441948248, -0.96144240441948248, 0.27500636898821867, 0, 0]--{230, 417}
2018-08-05 15:09:46.804831+0800 JJDynamics[13854:6043679] [0.20258440026074659, 0.97926480625568979, -0.97926480625568979, 0.20258440026074659, 0, 0]--{232, 421}
2018-08-05 15:09:46.821495+0800 JJDynamics[13854:6043679] [0.12903553968889034, 0.99163996969525026, -0.99163996969525026, 0.12903553968889034, 0, 0]--{235, 426}
2018-08-05 15:09:46.838145+0800 JJDynamics[13854:6043679] [0.054968607375915829, 0.99848808315530357, -0.99848808315530357, 0.054968607375915829, 0, 0]--{237, 430}
2018-08-05 15:09:46.854825+0800 JJDynamics[13854:6043679] [-0.019202492901692646, 0.99981561513429085, -0.99981561513429085, -0.019202492901692646, 0, 0]--{240, 435}
2018-08-05 15:09:46.871553+0800 JJDynamics[13854:6043679] [-0.093267919662992665, 0.99564104734675218, -0.99564104734675218, -0.093267919662992665, 0, 0]--{242, 440}
2018-08-05 15:09:46.888131+0800 JJDynamics[13854:6043679] [-0.16662288158149985, 0.98602069721354102, -0.98602069721354102, -0.16662288158149985, 0, 0]--{245, 446}
2018-08-05 15:09:46.904801+0800 JJDynamics[13854:6043679] [-0.23887162744301088, 0.97105115498758732, -0.97105115498758732, -0.23887162744301088, 0, 0]--{247, 451}
2018-08-05 15:09:46.921494+0800 JJDynamics[13854:6043679] [-0.30981996369562398, 0.95079524088819578, -0.95079524088819578, -0.30981996369562398, 0, 0]--{250, 457}
2018-08-05 15:09:46.938459+0800 JJDynamics[13854:6043679] [-0.37889656960767365, 0.92543902529531208, -0.92543902529531208, -0.37889656960767365, 0, 0]--{252, 463}
2018-08-05 15:09:46.954883+0800 JJDynamics[13854:6043679] [-0.44592163430196508, 0.89507200607630699, -0.89507200607630699, -0.44592163430196508, 0, 0]--{254, 470}
2018-08-05 15:09:46.971456+0800 JJDynamics[13854:6043679] [-0.51036026761936359, 0.85996069516896623, -0.85996069516896623, -0.51036026761936359, 0, 0]--{257, 476}
2018-08-05 15:09:46.988148+0800 JJDynamics[13854:6043679] [-0.57188648350497095, 0.82033276783529652, -0.82033276783529652, -0.57188648350497095, 0, 0]--{259, 483}
2018-08-05 15:09:47.004891+0800 JJDynamics[13854:6043679] [-0.63034976238832374, 0.77631126299763542, -0.77631126299763542, -0.63034976238832374, 0, 0]--{262, 491}
2018-08-05 15:09:47.021557+0800 JJDynamics[13854:6043679] [-0.68529134374358092, 0.72826902597193932, -0.72826902597193932, -0.68529134374358092, 0, 0]--{264, 498}
2018-08-05 15:09:47.038110+0800 JJDynamics[13854:6043679] [-0.73644734475166251, 0.6764948694618651, -0.6764948694618651, -0.73644734475166251, 0, 0]--{267, 506}
2018-08-05 15:09:47.054828+0800 JJDynamics[13854:6043679] [-0.78370201615604596, 0.6211369815692418, -0.6211369815692418, -0.78370201615604596, 0, 0]--{269, 514}
2018-08-05 15:09:47.072141+0800 JJDynamics[13854:6043679] [-0.82680502634812847, 0.56248862068976169, -0.56248862068976169, -0.82680502634812847, 0, 0]--{271, 522}
2018-08-05 15:09:47.088440+0800 JJDynamics[13854:6043679] [-0.86532762371592398, 0.50120664763563583, -0.50120664763563583, -0.86532762371592398, 0, 0]--{274, 531}
2018-08-05 15:09:47.104784+0800 JJDynamics[13854:6043679] [-0.89931636232794632, 0.4372986170218584, -0.4372986170218584, -0.89931636232794632, 0, 0]--{276, 540}
2018-08-05 15:09:47.121526+0800 JJDynamics[13854:6043679] [-0.92859315918310981, 0.37109937310420188, -0.37109937310420188, -0.92859315918310981, 0, 0]--{279, 549}
2018-08-05 15:09:47.138148+0800 JJDynamics[13854:6043679] [-0.95294400914857713, 0.30314635974696541, -0.30314635974696541, -0.95294400914857713, 0, 0]--{281, 558}
2018-08-05 15:09:47.154826+0800 JJDynamics[13854:6043679] [-0.97228272980628894, 0.2338082404887197, -0.2338082404887197, -0.97228272980628894, 0, 0]--{283, 568}
2018-08-05 15:09:47.171480+0800 JJDynamics[13854:6043679] [-0.98658331383196829, 0.16325858283205821, -0.16325858283205821, -0.98658331383196829, 0, 0]--{286, 578}
2018-08-05 15:09:47.188214+0800 JJDynamics[13854:6043679] [-0.99575326651152141, 0.092062110727676366, -0.092062110727676366, -0.99575326651152141, 0, 0]--{288, 588}
2018-08-05 15:09:47.205096+0800 JJDynamics[13854:6043679] [-0.99978797880165382, 0.020591198209523983, -0.020591198209523983, -0.99978797880165382, 0, 0]--{291, 598}
2018-08-05 15:09:47.221709+0800 JJDynamics[13854:6043679] [-0.99870015633651066, -0.050970557515386081, 0.050970557515386081, -0.99870015633651066, 0, 0]--{293, 609}
2018-08-05 15:09:47.238137+0800 JJDynamics[13854:6043679] [-0.99249492716280507, -0.122285810935277, 0.122285810935277, -0.99249492716280507, 0, 0]--{295, 620}
2018-08-05 15:09:47.254808+0800 JJDynamics[13854:6043679] [-0.98128089779382333, -0.19258192964280935, 0.19258192964280935, -0.98128089779382333, 0, 0]--{298, 631}
2018-08-05 15:09:47.271730+0800 JJDynamics[13854:6043679] [-0.96509442494820807, -0.26190217817705813, 0.26190217817705813, -0.96509442494820807, 0, 0]--{300, 643}
2018-08-05 15:09:47.288230+0800 JJDynamics[13854:6043679] [-0.94401753031973923, -0.32989529012858027, 0.32989529012858027, -0.94401753031973923, 0, 0]--{302, 654}
2018-08-05 15:09:47.304833+0800 JJDynamics[13854:6043679] [-0.91823624181158126, -0.39603308475901755, 0.39603308475901755, -0.91823624181158126, 0, 0]--{305, 666}
2018-08-05 15:09:47.321616+0800 JJDynamics[13854:6043679] [-0.91608428569548672, -0.40098576221829862, 0.40098576221829862, -0.91608428569548672, 0, 0]--{305, 669}
2018-08-05 15:09:47.338960+0800 JJDynamics[13854:6043679] [-0.92642517599358942, -0.37647894135694598, 0.37647894135694598, -0.92642517599358942, 0, 0]--{305, 667}
2018-08-05 15:09:47.354852+0800 JJDynamics[13854:6043679] [-0.93604024367110672, -0.35189296984755919, 0.35189296984755919, -0.93604024367110672, 0, 0]--{304, 666}
2018-08-05 15:09:47.375910+0800 JJDynamics[13854:6043679] [-0.94500296662989636, -0.32706175725800612, 0.32706175725800612, -0.94500296662989636, 0, 0]--{304, 664}
2018-08-05 15:09:47.388297+0800 JJDynamics[13854:6043679] [-0.95324667898583482, -0.30219326432347371, 0.30219326432347371, -0.95324667898583482, 0, 0]--{303, 663}
2018-08-05 15:09:47.404836+0800 JJDynamics[13854:6043679] [-0.96089148637345423, -0.2769251729529365, 0.2769251729529365, -0.96089148637345423, 0, 0]--{303, 663}
2018-08-05 15:09:47.421459+0800 JJDynamics[13854:6043679] [-0.96781631755177133, -0.25165765531874607, 0.25165765531874607, -0.96781631755177133, 0, 0]--{302, 662}
2018-08-05 15:09:47.439010+0800 JJDynamics[13854:6043679] [-0.97407683889910945, -0.22621739968516674, 0.22621739968516674, -0.97407683889910945, 0, 0]--{302, 662}
2018-08-05 15:09:47.457963+0800 JJDynamics[13854:6043679] [-0.97966875318901847, -0.20062186826234552, 0.20062186826234552, -0.97966875318901847, 0, 0]--{301, 662}
2018-08-05 15:09:47.471699+0800 JJDynamics[13854:6043679] [-0.9845532247099249, -0.17508554398718407, 0.17508554398718407, -0.9845532247099249, 0, 0]--{301, 663}
2018-08-05 15:09:47.488229+0800 JJDynamics[13854:6043679] [-0.98880204214124978, -0.14923311112917995, 0.14923311112917995, -0.98880204214124978, 0, 0]--{300, 664}
2018-08-05 15:09:47.504880+0800 JJDynamics[13854:6043679] [-0.99234746962863918, -0.12347671651626085, 0.12347671651626085, -0.99234746962863918, 0, 0]--{300, 665}
2018-08-05 15:09:47.521559+0800 JJDynamics[13854:6043679] [-0.99522210801562294, -0.097636856345028361, 0.097636856345028361, -0.99522210801562294, 0, 0]--{299, 666}
2018-08-05 15:09:47.538173+0800 JJDynamics[13854:6043679] [-0.99740964800825904, -0.071930480743846309, 0.071930480743846309, -0.99740964800825904, 0, 0]--{299, 668}
2018-08-05 15:09:47.554884+0800 JJDynamics[13854:6043679] [-0.99894252433625585, -0.045976440409288853, 0.045976440409288853, -0.99894252433625585, 0, 0]--{298, 669}
2018-08-05 15:09:47.571560+0800 JJDynamics[13854:6043679] [-0.99979613529768074, -0.020191281381371936, 0.020191281381371936, -0.99979613529768074, 0, 0]--{298, 671}
2018-08-05 15:09:47.588330+0800 JJDynamics[13854:6043679] [-0.99998436115367473, 0.0055926244355460411, -0.0055926244355460411, -0.99998436115367473, 0, 0]--{297, 674}
2018-08-05 15:09:47.604888+0800 JJDynamics[13854:6043679] [-0.99950729111595327, 0.03138749760731336, -0.03138749760731336, -0.99950729111595327, 0, 0]--{297, 676}
2018-08-05 15:09:47.621514+0800 JJDynamics[13854:6043679] [-0.99836494594901304, 0.05716147916406935, -0.05716147916406935, -0.99836494594901304, 0, 0]--{296, 679}
2018-08-05 15:09:47.638316+0800 JJDynamics[13854:6043679] [-0.99657464555281372, 0.08269810059054418, -0.08269810059054418, -0.99657464555281372, 0, 0]--{296, 683}
2018-08-05 15:09:47.655573+0800 JJDynamics[13854:6043679] [-0.99879028539667847, 0.049172815632435626, -0.049172815632435626, -0.99879028539667847, 0, 0]--{296, 683}
2018-08-05 15:09:47.671630+0800 JJDynamics[13854:6043679] [-0.99986890184941479, 0.016191945974629109, -0.016191945974629109, -0.99986890184941479, 0, 0]--{296, 684}
2018-08-05 15:09:47.688278+0800 JJDynamics[13854:6043679] [-0.99985900670601602, -0.016791864365193226, 0.016791864365193226, -0.99985900670601602, 0, 0]--{296, 685}
2018-08-05 15:09:47.704881+0800 JJDynamics[13854:6043679] [-0.99995007385255941, -0.0099924872910074139, 0.0099924872910074139, -0.99995007385255941, 0, 0]--{296, 685}
2018-08-05 15:09:47.721717+0800 JJDynamics[13854:6043679] [-0.99999490348585685, -0.0031926481659901476, 0.0031926481659901476, -0.99999490348585685, 0, 0]--{295, 685}
2018-08-05 15:09:47.738278+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.754951+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.771863+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.788082+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.804749+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.821476+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.838159+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.854808+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.871393+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.888066+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.904744+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.921494+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.938082+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.954743+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.971479+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:47.988089+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.004811+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.021453+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.038136+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.054788+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.071459+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.088120+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.104763+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.121434+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.138105+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.154762+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.171488+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.188160+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.204820+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.221486+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.238122+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.254783+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.271448+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
2018-08-05 15:09:48.288107+0800 JJDynamics[13854:6043679] [-0.99999998144229718, 0.00019265358860153227, -0.00019265358860153227, -0.99999998144229718, 0, 0]--{295, 685}
雖然動力學(xué)適用于這些屬性的確切值可能沒什么意義晨继,但重要的是要知道它們正在被應(yīng)用。 因此搬俊,如果以編程方式更改對象的frame或變換屬性紊扬,則可能會覆蓋這些值。 這意味著在動態(tài)控制下唉擂,您無法使用變換來縮放對象餐屎。
動態(tài)行為的方法簽名使用term item
而不是視圖。 將動態(tài)行為應(yīng)用于對象的唯一要求是它采用UIDynamicItem
協(xié)議玩祟,如下所示:
protocol UIDynamicItem : NSObjectProtocol {
var center: CGPoint { get set }
var bounds: CGRect { get }
var transform: CGAffineTransform { get set }
}
UIDynamicItem
協(xié)議動態(tài)的讀寫center
和transform
屬性腹缩,允許它根據(jù)內(nèi)部計算移動item。 它還具有對邊界的讀訪問權(quán)限空扎,用于確定item的size藏鹊。 這允許它圍繞物品的周邊創(chuàng)建碰撞邊界,并在施加力時計算物品的質(zhì)量转锈。
該協(xié)議意味著dynamics
與UIView沒有緊密耦合盘寡,確實有另一個UIKit類不是視圖但仍采用此協(xié)議:UICollectionViewLayoutAttributes
。 這允許dynamics
動畫collection views
的item撮慨。
Collision notifications - Collision通知
到目前為止竿痰,您已添加了一些視圖和行為,然后讓dynamics
接管甫煞。 在下一步中菇曲,您將了解如何在items碰撞時接收通知。
Swift版本
仍然在ViewController.swift
中抚吠,通過更新類聲明采用UICollisionBehaviorDelegate
協(xié)議:
class ViewController: UIViewController, UICollisionBehaviorDelegate {
在viewDidLoad
中常潮,在初始化碰撞對象之后將視圖控制器設(shè)置為代理,如下所示:
collision.collisionDelegate = self
接下來楷力,將一個碰撞行為代理方法的實現(xiàn)添加到類中:
func collisionBehavior(behavior: UICollisionBehavior!, beganContactForItem item: UIDynamicItem!, withBoundaryIdentifier identifier: NSCopying!, atPoint p: CGPoint) {
println("Boundary contact occurred - \(identifier)")
}
發(fā)生碰撞時會調(diào)用此代理方法喊式。 它會將一條日志消息打印到控制臺。 為了避免使用大量消息混亂控制臺日志萧朝,請刪除您在上面添加的collision.action
日志記錄岔留。
建立并運行,您的對象將進行交互检柬,您將在控制臺中看到以下條目:
Boundary contact occurred - barrier
Boundary contact occurred - barrier
Boundary contact occurred - nil
Boundary contact occurred - nil
Boundary contact occurred - nil
Boundary contact occurred - nil
從上面的日志消息中你可以看到方塊視圖與邊界標(biāo)識符屏障碰撞兩次献联,這是您之前添加的無形邊界。 (null)標(biāo)識符引用外部參考視圖邊界。
這些日志消息讀起來很有意思里逆,但是當(dāng)item反彈時提供視覺指示會更有趣进胯。
在將消息發(fā)送到日志的行下方,添加以下內(nèi)容:
let collidingView = item as UIView
collidingView.backgroundColor = UIColor.yellowColor()
UIView.animateWithDuration(0.3) {
collidingView.backgroundColor = UIColor.grayColor()
}
上面的代碼將碰撞item的背景顏色更改為黃色原押,然后再將其淡化為灰色胁镐。
Build并run以查看此效果:
每次碰到邊界時,方塊視圖都會閃爍黃色诸衔。
到目前為止盯漂,UIKit Dynamics已根據(jù)item的界限自動設(shè)定item的物理屬性(如質(zhì)量和彈性)。 接下來笨农,您將看到如何使用UIDynamicItemBehavior
類自己控制這些物理屬性就缆。
OC版本
@interface ViewController () <UICollisionBehaviorDelegate>
collision.collisionDelegate = self;
#pragma mark - UICollisionBehaviorDelegate
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p
{
NSLog(@"Boundary contact occurred - %@", identifier);
}
下面看一下輸出結(jié)果
2018-08-05 15:56:25.638287+0800 JJDynamics[13861:6057467] Boundary contact occurred - barrier
2018-08-05 15:56:25.638399+0800 JJDynamics[13861:6057467] Boundary contact occurred - barrier
下面我們也看一下那個碰撞就變色的效果
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p
{
if ([item isKindOfClass:[UIView class]]) {
UIView *colorItem = (UIView *)item;
colorItem.backgroundColor = [UIColor blueColor];
[UIView animateWithDuration:0.3 animations:^{
colorItem.backgroundColor = [UIColor redColor];
}];
}
}
看一下效果輸出,可以看見碰撞的時候變成了藍(lán)色磁餐,碰撞結(jié)束后就變成了紅色违崇。
Configuring item properties - 配置item屬性
Swift版本
在viewDidLoad
中,將以下內(nèi)容添加到方法的末尾:
let itemBehaviour = UIDynamicItemBehavior(items: [square])
itemBehaviour.elasticity = 0.6
animator.addBehavior(itemBehaviour)
上面的代碼創(chuàng)建了一個項目行為诊霹,將其與方塊視圖相關(guān)聯(lián)羞延,然后將行為對象添加到animator
。 elasticity
屬性控制item的彈性; 值1.0表示完全彈性碰撞脾还;也就是說伴箩,碰撞中沒有能量或速度損失。 您已將方塊視圖的彈性設(shè)置為0.6鄙漏,這意味著每次反彈時方塊都會失去速度嗤谚。
Build并運行您的應(yīng)用程序,您會注意到該方塊視圖現(xiàn)在表現(xiàn)得更為方式怔蚌,如下所示:
注意:如果您想知道我是如何使用顯示方形視圖先前位置的軌跡生成上述圖像的巩步,那實際上非常簡單! 只是將一個block添加到其中一個行為的action屬性中桦踊,并且每三次執(zhí)行block代碼椅野,使用方形視圖的當(dāng)前center和transform向視圖添加一個新的方形視圖。 代碼大致如下籍胯。
var updateCount = 0
collision.action = {
if (updateCount % 3 == 0) {
let outline = UIView(frame: square.bounds)
outline.transform = square.transform
outline.center = square.center
outline.alpha = 0.5
outline.backgroundColor = UIColor.clearColor()
outline.layer.borderColor = square.layer.presentationLayer().backgroundColor
outline.layer.borderWidth = 1.0
self.view.addSubview(outline)
}
++updateCount
}
在上面的代碼中你只改變了item的彈性竟闪;但是,該item的行為類具有許多可以在代碼中操作的其他屬性杖狼。它們?nèi)缦拢?/p>
- elasticity - 彈性 —— 確定“彈性”碰撞的方式炼蛤,即物品在碰撞中的彈性或“橡皮”狀態(tài)。
- friction - 摩擦力 —— 確定沿表面滑動時的運動阻力蝶涩。
- density - 密度 —— 當(dāng)與尺寸size結(jié)合時理朋,這將給出item的總質(zhì)量絮识。質(zhì)量越大,加速或減速物體越難嗽上。
- resistance - 阻力 —— 確定任何線性運動的阻力笋除。這與摩擦力形成對比,摩擦力僅適用于滑動運動炸裆。
- 角度阻力 - angularResistance —— 確定任何旋轉(zhuǎn)運動的阻力。
- 允許旋轉(zhuǎn) - allowsRotation —— 這是一個有趣的鲜屏,不會模擬任何真實世界的物理屬性烹看。將此屬性設(shè)置為NO時,無論發(fā)生任何旋轉(zhuǎn)力洛史,對象都不會旋轉(zhuǎn)惯殊。
OC版本
首先,我們也添加下elasticity
這個屬性也殖,在viewDidLoad最后添加如下代碼土思。
UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.dynamicView]];
itemBehavior.elasticity = 0.6;
[animator addBehavior:itemBehavior];
下面運行看一下效果圖
接下來,我們也繪制出方形視圖的運動軌跡忆嗜。
@property (nonatomic, assign) NSInteger number;
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.dynamicView]];
collision.action = ^{
if (self.number % 3 == 0) {
UIView *view = [[UIView alloc] initWithFrame:self.dynamicView.bounds];
view.transform = self.dynamicView.transform;
view.center = self.dynamicView.center;
view.alpha = 0.5;
view.backgroundColor = [UIColor clearColor];
view.layer.borderColor = self.dynamicView.layer.presentationLayer.backgroundColor;
view.layer.borderWidth = 1.0;
[self.view addSubview:view];
}
self.number ++;
};
下面看一下軌跡示意圖
Adding behaviors dynamically - 動態(tài)添加行為
在當(dāng)前狀態(tài)下己儒,您的應(yīng)用程序會設(shè)置系統(tǒng)的所有行為,然后讓動態(tài)處理系統(tǒng)的物理特性捆毫,直到所有項目都停止闪湾。 在下一步中,您將看到如何動態(tài)添加和刪除行為绩卤。
Swift版本
下面在viewDidLoad
上面添加以下屬性:
var firstContact = false
將以下代碼添加到碰撞代理方法collisionBehavior(behavior:beganContactForItem:withBoundaryIdentifier:atPoint:)
的末尾途样。
if (!firstContact) {
firstContact = true
let square = UIView(frame: CGRect(x: 30, y: 0, width: 100, height: 100))
square.backgroundColor = UIColor.grayColor()
view.addSubview(square)
collision.addItem(square)
gravity.addItem(square)
let attach = UIAttachmentBehavior(item: collidingView, attachedToItem:square)
animator.addBehavior(attach)
}
上面的代碼檢測障礙物和方形視圖之間的初始接觸,創(chuàng)建第二個方形視圖并將其添加到碰撞和重力行為中濒憋。 此外何暇,您還可以設(shè)置attachment
行為以創(chuàng)建使用虛擬彈簧附加到對象的效果。
構(gòu)建并運行您的應(yīng)用程序; 當(dāng)原始方塊視圖撞到障礙物時凛驮,您應(yīng)該會看到一個新的方塊裆站,如下所示:
雖然兩個正方形視圖之間似乎存在連接,但實際上您無法將連接視為線或彈簧辐烂,因為屏幕上沒有繪制任何內(nèi)容來表示它遏插。
OC版本
首先在sb中拖進去一個新的view,并設(shè)置約束
首先我們也添加下代碼
@property (nonatomic, assign) BOOL isFirstContact;
@property (weak, nonatomic) IBOutlet UIView *anotherView;
#pragma mark - UICollisionBehaviorDelegate
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p
{
UIView *colorItem;
if ([item isKindOfClass:[UIView class]]) {
colorItem = (UIView *)item;
colorItem.backgroundColor = [UIColor blueColor];
[UIView animateWithDuration:0.3 animations:^{
colorItem.backgroundColor = [UIColor redColor];
}];
}
if (self.isFirstContact == NO) {
self.isFirstContact = YES;
[self.collision addItem:self.anotherView];
[self.gravity addItem:self.anotherView];
UIAttachmentBehavior *attachBehavior = [[UIAttachmentBehavior alloc] initWithItem:colorItem attachedToItem:self.anotherView];
[self.animator addBehavior:attachBehavior];
}
}
下面看一下效果
User Interaction - 用戶交互
Swift版本
如您所見纠修,您可以在物理系統(tǒng)運動時動態(tài)添加和刪除行為胳嘲。 在最后一節(jié)中,只要用戶點擊屏幕扣草,您就會添加另一種動態(tài)行為UISnapBehavior
了牛。 UISnapBehavior
使一個對象跳轉(zhuǎn)到指定的位置颜屠,具有類似彈簧的動畫。
刪除您在上一節(jié)中添加的代碼:collisionBehavior()
中的firstContact
屬性和if語句鹰祸。 在屏幕上只有一個方形視圖甫窟,更容易看到UISnapBehavior
的效果。
在viewDidLoad上方添加兩個屬性:
var square: UIView!
var snap: UISnapBehavior!
這將跟蹤您的方形視圖蛙婴,以便您可以從視圖控制器中的其他位置訪問它粗井。 接下來你將使用snap
對象。
在viewDidLoad中街图,從square的聲明中刪除let關(guān)鍵字浇衬,以便它使用new屬性而不是局部變量:
square = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
最后,為touchesEnded
添加一個實現(xiàn)餐济,以便在用戶觸摸屏幕時創(chuàng)建并添加新的snap行為:
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
if (snap != nil) {
animator.removeBehavior(snap)
}
let touch = touches.anyObject() as UITouch
snap = UISnapBehavior(item: square, snapToPoint: touch.locationInView(view))
animator.addBehavior(snap)
}
這段代碼非常簡單耘擂。 首先,它檢查是否存在現(xiàn)有的snap行為并將其刪除絮姆。 然后創(chuàng)建一個新的snap行為醉冤,將方形視圖捕捉到用戶觸摸的位置,并將其添加到animator
篙悯。
構(gòu)建并運行您的應(yīng)用程序蚁阳。 嘗試點擊方形視圖應(yīng)該放大到任何你觸摸的地方!
OC版本
直接看代碼吧
@property (weak, nonatomic) IBOutlet UIView *dynamicView;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (self.snapBehavior != nil) {
[self.animator removeBehavior:self.snapBehavior];
}
UITouch *viewTouch = (UITouch *)touches.anyObject;
self.snapBehavior = [[UISnapBehavior alloc] initWithItem:self.dynamicView snapToPoint:[viewTouch locationInView:self.view]];
[self.animator addBehavior:self.snapBehavior];
}
下面看一下實際效果
這樣它就跟著你點擊的位置進行移動了辕近。
后記
本篇主要講述了UIKit動力學(xué)和移動效果韵吨,感興趣的給個贊或者關(guān)注~~~~