The uidynamic of iOS game development
Last Update:2015-06-18
Source: Internet
Author: User
<span id="Label3"></p>Introduction to the Uidynamic of iOS game development <ul> <ul> <li><p>What is Uidynamic</p> <ul> <li>Uidynamic is a new technology introduced from iOS 7 and belongs to the Uikit framework</li> <li>Can be thought of as a physical engine capable of simulating and simulating physical phenomena in real life.<code>重力、弹性碰撞等现象</code></li> </ul></li> <li><p>The value of the physics engine</p> <ul> <li>Widely used in game development, the classic success story is "angry birds"</li> <li>Enable developers to achieve cool physics simulations away from the physics equation</li> <li>Improve the game development efficiency, produce more excellent fun physics simulation game</li> </ul></li> </ul> </ul>Well-known 2D physics engine <ul> <ul> <li>Box2d</li> <li>Chipmunk</li> </ul> </ul>Use steps <ul> <ul> <li>To use uidynamic to achieve physical simulation, the following steps are outlined below</li> <li>Create a physical emulator (by the way, set the simulation Scope)</li> <li>Create the appropriate physical simulation behavior (by Adding physical simulation elements by the Way)</li> <li>Add physical emulation behavior to the physical emulator? Start simulation</li> </ul> </ul>Three major concepts <ul> <ul> <li><p>Physical emulation element (Dynamic Item)</p> <ul> <li>Who wants to perform physical simulations?</li> </ul></li> <li><p>Physical simulation Behavior (Dynamic Behavior)</p> <ul> <li>What kind of physical simulation effect does it perform? What kind of animated effect?</li> </ul></li> <li><p>Physical Emulator (Dynamic Animator)</p> <ul> <li>Allow physical simulation elements to perform specific physical simulation behavior</li> </ul></li> </ul> </ul>Physical simulation Elements Note <ul> <ul> <li>Not all objects can do physical simulation elements</li> <li>Not all objects can be physically emulated</li> </ul> </ul>Which objects can make physical simulation elements <ul> <ul> <li>Any object that complies with the Uidynamicitem protocol</li> <li>UIView has complied with the Uidynamicitem protocol by default, so any UI control can do a physical simulation</li> <li>The Uicollectionviewlayoutattributes class also complies with the Uidynamicitem protocol by default</li> </ul> </ul>Physical simulation Behavior <ul> <ul> <li><p>The uidynamic provides several physical simulation behaviors</p> <ul> <li>Uigravitybehavior: Gravity behavior</li> <li>Uicollisionbehavior: Collision Behavior</li> <li>Uisnapbehavior: snapping behavior</li> <li>Uipushbehavior: Driving Behavior</li> <li>uiattachmentbehavior: adhesion behavior</li> <li>Uidynamicitembehavior: Dynamic Elemental Behavior</li> </ul></li> <li><p>Physical Simulation Behavior Notice</p> <ul> <li>All of the above physical simulation behaviors are inherited from Uidynamicbehavior</li> <li>All Uidynamicbehavior can be carried out independently.</li> <li>When combined with multiple behaviors, you can achieve some more complex effects</li> </ul></li> </ul> </ul>Physical Emulator <ul> <ul> <li><p>Physical Emulator Notes</p> <ul> <li>It allows physical simulation elements to perform physical simulation behavior</li> <li>It is an object of type Uidynamicanimator</li> </ul></li> <li><p>Initialization of the Uidynamicanimator</p></li> </ul> </ul><pre><pre><code class="lang-objc"><span class="hljs-comment">//view参数:是一个参照视图,表示物理仿真的范围- (instancetype)initWithReferenceView:(<span class="hljs-built_in">UIView *)view;</span></span></code></pre></pre> <ul> <ul> <li>Common methods of Uidynamicanimator</li> </ul> </ul><pre><pre><code class="lang-objc"><span class="hljs-comment">//添加1个物理仿真行为- (<span class="hljs-keyword">void)addBehavior:(UIDynamicBehavior *)behavior;</span></span></code></pre></pre><pre><pre><code class="lang-objc"><span class="hljs-comment">//移除1个物理仿真行为- (<span class="hljs-keyword">void)removeBehavior:(UIDynamicBehavior *)behavior;</span></span></code></pre></pre><pre><pre><code class="lang-objc"><span class="hljs-comment">//移除之前添加过的所有物理仿真行为- (<span class="hljs-keyword">void)removeAllBehaviors;</span></span></code></pre></pre> <ul> <ul> <li>Common Properties of Uidynamicanimator</li> </ul> </ul><pre><pre><code class="lang-objc"><span class="hljs-comment">//参照视图<span class="hljs-keyword">@property (<span class="hljs-keyword">nonatomic, <span class="hljs-keyword">readonly) <span class="hljs-built_in">UIView* referenceView;</span></span></span></span></span></code></pre></pre><pre><pre><code class="lang-objc"><span class="hljs-comment">//添加到物理仿真器中的所有物理仿真行为<span class="hljs-keyword">@property (<span class="hljs-keyword">nonatomic, <span class="hljs-keyword">readonly, <span class="hljs-keyword">copy) <span class="hljs-built_in">NSArray* behaviors;</span></span></span></span></span></span></code></pre></pre><pre><pre><code class="lang-objc"><span class="hljs-comment">//是否正在进行物理仿真<span class="hljs-keyword">@property (<span class="hljs-keyword">nonatomic, <span class="hljs-keyword">readonly, getter = isRunning) <span class="hljs-built_in">BOOL running;</span></span></span></span></span></code></pre></pre><pre><pre><code class="lang-objc"><span class="hljs-comment">//代理对象(能监听物理仿真器的仿真过程,比如开始和结束)<span class="hljs-keyword">@property (<span class="hljs-keyword">nonatomic, <span class="hljs-keyword">assign) <span class="hljs-keyword">id <UIDynamicAnimatorDelegate> delegate;</span></span></span></span></span></code></pre></pre>Introduction to Gravity Behavior (uigravityanimator)<pre><pre><code>给定重力方向、加速度,让物体朝着重力方向掉落</code></pre></pre> <ul> <ul> <li>Initialization of the Uigravitybehavior</li> </ul> </ul><pre><pre><code class="lang-objc"><span class="hljs-comment">//item参数 :里面存放着物理仿真元素- (instancetype)initWithItems:(<span class="hljs-built_in">NSArray *)items;</span></span></code></pre></pre> <ul> <ul> <li>Uigravitybehavior Common methods</li> </ul> </ul><pre><pre><code class="lang-objc"><span class="hljs-comment">//添加1个物理仿真元素- (<span class="hljs-keyword">void)addItem:(<span class="hljs-keyword">id <UIDynamicItem>)item;</span></span></span></code></pre></pre><pre><pre><code class="lang-objc"><span class="hljs-comment">//移除1个物理仿真元素- (<span class="hljs-keyword">void)removeItem:(<span class="hljs-keyword">id <UIDynamicItem>)item;</span></span></span></code></pre></pre> <ul> <ul> <li>Uigravitybehavior Common Properties</li> </ul> </ul><pre><pre><code class="lang-objc"><span class="hljs-comment">//添加到重力行为中的所有物理仿真元素<span class="hljs-keyword">@property (<span class="hljs-keyword">nonatomic, <span class="hljs-keyword">readonly, <span class="hljs-keyword">copy) <span class="hljs-built_in">NSArray* items;</span></span></span></span></span></span></code></pre></pre><pre><pre><code class="lang-objc"><span class="hljs-comment">//重力方向(是一个二维向量)<span class="hljs-keyword">@property (<span class="hljs-keyword">readwrite, <span class="hljs-keyword">nonatomic) CGVector gravityDirection;</span></span></span></span></code></pre></pre><pre><pre><code class="lang-objc"><span class="hljs-comment">//重力方向(是一个角度,以x轴正方向为0°,顺时针正数,逆时针负数)<span class="hljs-keyword">@property (<span class="hljs-keyword">readwrite, <span class="hljs-keyword">nonatomic) <span class="hljs-built_in">CGFloat angle;</span></span></span></span></span></code></pre></pre><pre><pre><code class="lang-objc"><span class="hljs-comment">//量级(用来控制加速度,1.0代表加速度是1000 points /second²)<span class="hljs-keyword">@property (<span class="hljs-keyword">readwrite, <span class="hljs-keyword">nonatomic) <span class="hljs-built_in">CGFloat magnitude;</span></span></span></span></span></code></pre></pre>Introduction to Collision Behavior (uicollisionbehavior)<pre><pre><code>可以让物体之间实现碰撞效果可以通过添加边界(boundary),让物理碰撞局限在某个空间中</code></pre></pre> <ul> <ul> <li>Uicollisionbehavior boundary-related Methods</li> </ul> </ul><pre><code class="lang-objc"><code class="lang-objc">-(<span class="hljs-keyword">void) addboundarywithidentifier: ( <span class="hljs-keyword">id <NSCopying>) identifier forpath: (uibezierpath*) bezierpath;-(<span class=" Hljs-keyword ">void) addboundarywithidentifier: (<span class=" hljs-keyword ">id <NSCopying>) identifier Frompoint: (<span class="hljs-built_in">cgpoint) p1 topoint: (<span class="hljs-built_in">cgpoint) p2;-( Uibezierpath*) boundarywithidentifier: (<span class="hljs-keyword">id <NSCopying>) identifier;-(<span class=" Hljs-keyword ">void) removeboundarywithidentifier: (<span class=" hljs-keyword ">id <NSCopying>) identifier;< Span class= "hljs-keyword" > @property (<span class="hljs-keyword">nonatomic, <span class="hljs-keyword">readonly, Span class= "hljs-keyword" >copy) <span class="hljs-built_in">nsarray* boundaryidentifiers;-( <span class="hljs-keyword">void) removeallboundaries; </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></code></code></pre> <ul> <ul> <li>Uicollisionbehavior Common usage</li> </ul> </ul><pre><pre><code class="lang-objc"><span class="hljs-comment">//是否以参照视图的bounds为边界<span class="hljs-keyword">@property (<span class="hljs-keyword">nonatomic, <span class="hljs-keyword">readwrite) <span class="hljs-built_in">BOOL translatesReferenceBoundsIntoBoundary;</span></span></span></span></span></code></pre></pre><pre><pre><code class="lang-objc"><span class="hljs-comment">//设置参照视图的bounds为边界,并且设置内边距-(<span class="hljs-keyword">void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(<span class="hljs-built_in">UIEdgeInsets)insets;</span></span></span></code></pre></pre><pre><pre><code class="lang-objc"><span class="hljs-comment">//碰撞模式(分为3种,元素碰撞、边界碰撞、全体碰撞)<span class="hljs-keyword">@property (<span class="hljs-keyword">nonatomic, <span class="hljs-keyword">readwrite) UICollisionBehaviorMode collisionMode;</span></span></span></span></code></pre></pre><pre><pre><code class="lang-objc"><span class="hljs-comment">//代理对象(可以监听元素的碰撞过程)<span class="hljs-keyword">@property (<span class="hljs-keyword">nonatomic, <span class="hljs-keyword">assign, <span class="hljs-keyword">readwrite) <span class="hljs-keyword">id <UICollisionBehaviorDelegate> collisionDelegate;</span></span></span></span></span></span></code></pre></pre>Introduction to snapping behavior (uisnapbehavior)<pre><pre><code>可以让物体迅速冲到某个位置(捕捉位置),捕捉到位置之后会带有一定的震动</code></pre></pre> <ul> <ul> <li>Initialization of the Uisnapbehavior</li> </ul> </ul><pre><pre><code class="lang-objc">- (instancetype)initWithItem:(<span class="hljs-keyword">id <UIDynamicItem>)item snapToPoint:(<span class="hljs-built_in">CGPoint)point;</span></span></code></pre></pre> <ul> <ul> <li>Uisnapbehavior Common Properties</li> </ul> </ul><pre><pre><code class="lang-objc"><span class="hljs-comment">//用于减幅、减震(取值范围是0.0 ~ 1.0,值越大,震动幅度越小)<span class="hljs-keyword">@property (<span class="hljs-keyword">nonatomic, <span class="hljs-keyword">assign) <span class="hljs-built_in">CGFloat damping;</span></span></span></span></span></code></pre></pre>Uisnapbehavior Use note<pre><pre><code>如果要进行连续的捕捉行为,需要先把前面的捕捉行为从物理仿真器中移除</code></pre></pre><p><p>The uidynamic of iOS game development</p></p></span>