Block是iOS 4.0 以後才有的語法, 看起來就像一個function.可以傳送參數至block,也可以從block回傳值.跟一般的function的差別在於,block可以被定義在"函式"與"方法"中,並且存取任意定義在有效範圍block外的變數.
其語法如下:
其語法如下:
Example
用block的作法如下:
UIView Animation
Animation class method in UIView
UIViewAnimationOptions
另外常用的還有(void)transitionWithView:和(void)transitionFromView: toView:.
另外常用的還有(void)transitionWithView:和(void)transitionFromView: toView:.
Dynamic Animation
Set up physics relating animatable objects and let them run until they resolve to stasis.Easily possible to set it up so that stasis never occurs, but that could be performance problem.只要物件繼承UIView類別,就自動擁有動態行為的能力.
STEPS
1. Create a UIDynamicAnimatorUIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:aView]; #If animating views, all views must be in a view hierarchy with reference view at the top.2. Add UIDynamicBehaviors to it (gravity, collisions, etc.)
#Create and add UIDynamicBehaviors. UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init]; [animator addBehavior:gravity];3. Add UIDynamicItems (usually UIViews) to the UIDynamicBehaviors.
id <UIDynamicItem> item2 = ...; [gravity addItem:item2];#The items have to implement the UIDynamicItem protocol ... @protocol UIDynamicItem @property (readonly) CGRect bounds; #drawing area of the item. @property (readwrite) CGPoint center; #the position of the item. @property (readwrite) CGAffineTransform transform; @end-(void)updateItemUsingCurrentState:(id <UIDynamicItem>)item; #If you change center or transform while animator is running, you must call UIDynamicAnimator’s
UIGravityBehavior
模擬物件受到引力後的等加速度運動.@property CGFloat angle; #設定重力的方向 @property CGFloat magnitude; 1.0 is 1000 points/s/s
UICollisionBehavior
碰撞時的行為,相當於讓物件的移動有個邊界範圍.例如球掉到地上後會彈跳一下下再停止.@property UICollisionBehaviorMode collisionMode; # Items, Boundaries, Everything (default) #定義item之間是相互碰撞彈開或是只從邊界碰撞彈開. - (void)addBoundaryWithIdentifier:(NSString *)identifier forPath:(UIBezierPath *)path; @property BOOL translatesReferenceBoundsIntoBoundary; #設定邊界
UIAttachmentBehavior
讓兩個物件(其中一個可以是某個座標點),讓他們互相黏起來.只要有一個物件移動,另外一個就會跟著動.- (instancetype)initWithItem:(id <UIDynamicItem>)item attachedToAnchor:(CGPoint)anchor; #不用alloc/init初始化而是搭配initwithItem. - (instancetype)initWithItem:(id <UIDynamicItem>)i1 attachedToItem:(id <UIDynamicItem>)i2; - (instancetype)initWithItem:(id <UIDynamicItem>)item offsetFromCenter:(CGPoint)offset ... @property (readwrite) CGFloat length; // distance between attached things (settable!) Can also control damping and frequency of oscillations. @property (readwrite) CGPoint anchorPoint; // can be reset at any time
UISnapBehavior
設定一個座標點後,物件會自動“撲向”那個座標點.
UIPushBehavior
將物件往某一個方向推動.有兩種施力方式,一種為推一下就停止,物件就會依照慣性原理等速度移動.另一種則是推力一直不斷的加在物件上,一直維持著等速度的移動.@property UIPushBehaviorMode mode; # Continuous or Instantaneous @property CGVector pushDirection; @property CGFloat magnitude/angle; # magnitude 1.0 moves a 100x100 view at 100 pts/s/sthe behavior will be sent this message when its animator changes.
- (void)willMoveToAnimator:(UIDynamicAnimator *)animator;
範例:
參考網址:
Standford CS193P
http://adoptioncurve.net/archives/2013/02/blocks-in-objective-c-without-tears/
沒有留言:
張貼留言