標籤:
UIDynamic 是從iOS 7開始引入的一種新技術,屬於UIKit架構,可以類比現實生活中的物理現象,如:碰撞、抖動、擺動等
動力效果:有一個效果器,叫做“動力效果器”裡面面可以添加“動力效果”
動力效果:可以將上一個效果移除
動力效果:可以疊加多個效果
------------------------------------------
使用UIDynamic 基本步驟:
1、建立一個動力效果器(UIDynamicAnimator)
2、建立動力效果(Behavior)添加到對應的視圖上
3、將動力效果添加到動力效果器中(開始動力效果)
必須遵循UIDynamicItem這個協議才可以使用動力效果
UIView預設遵守UIDynamicItem協議
------------------------------------------
UIDynamic提供的動力效果
UIGravityBehavior:重力效果
UICollisionBehavior:碰撞效果
UIDynamicItemBehavior:動力元素效果
UIPushBehavior:推動效果
UISnapBehavior:迅速移動效果
UIAttachmentBehavior:附著效果
都繼承自UIDynamicBehavior
------------------------------------------
動力效果器:UIDynamicAnimator
可以把UIDynamicAnimator看做動力效果的容器 它制定了動力效果的有效範圍
在初始化的時候可以指定他的有效範圍
- (instancetype)initWithReferenceView:(UIView*)view;
作用在哪一個view上 哪一個view就是他產生動力效果的有效範圍
------------------------------------------
動力效果器常用的屬性
@property (nonatomic, readonly) UIView* referenceView;作用的地區
@property (nonatomic, readonly, copy) NSArray* behaviors;添加到效果器中的所有效果
@property (nonatomic, readonly, getter = isRunning) BOOL running;是否進行中
@property (nonatomic, assign) id <UIDynamicAnimatorDelegate> delegate;可以檢測開始暫停
- (void)dynamicAnimatorWillResume:(UIDynamicAnimator*)animator;
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator*)animator;
------------------------------------------
既然是容器 他還可以添加移除 動力效果
- (void)addBehavior:(UIDynamicBehavior *)behavior; 添加動力效果
- (void)removeBehavior:(UIDynamicBehavior *)behavior; 移除動力效果
- (void)removeAllBehaviors; 移除之前添加過的所有動力效果
------------------------------------------
Behavior:動力效果
1、重力效果:UIGravityBehavior
設定重力方向、加速度、讓物體(視圖)朝著重力方向掉落
常用屬性:
@property (nonatomic, readonly, copy) NSArray *items;添加到重力效果中的所有效果作用對象
@property (readwrite, nonatomic) CGVector gravityDirection;重力方向(是一個二維向量)以左上方為座標原點 x 負數向左 正數向右 y 負數向上 正數向下 數字越大 重力效果越大
@property (readwrite, nonatomic) CGFloat angle;重力方向(是一個角度,x軸正方向為0°,順時針正數,逆時針負數)
@property (readwrite, nonatomic) CGFloat magnitude;量級(用來控制加速度,1.0代表加速度是1000 points /second²)重力加速度越大 碰撞越厲害
------------------------------------------
2、UICollisionBehavior: 碰撞效果
可以讓物體之間實現碰撞效果
也可以通過添加邊界(boundary)在邊界實現碰撞效果
常用屬性:
@property (nonatomic, readonly, copy) NSArray* items;
@property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;
@property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;
@property (nonatomic, readonly, copy) NSArray* boundaryIdentifiers;
@property (nonatomic, assign, readwrite) id <UICollisionBehaviorDelegate> collisionDelegate;
邊界相關的方法
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier forPath:(UIBezierPath*)bezierPath; 添加一個貝茲路徑路徑的邊界
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2; 通過添加兩個點連成的線 作為邊界
- (UIBezierPath*)boundaryWithIdentifier:(id <NSCopying>)identifier; 通過ID找到邊界路徑
- (void)removeBoundaryWithIdentifier:(id <NSCopying>)identifier; 移除ID對應的邊界
@property (nonatomic, readonly, copy) NSArray* boundaryIdentifiers; 邊界數組
- (void)removeAllBoundaries;移除所有邊界
typedef NS_OPTIONS(NSUInteger, UICollisionBehaviorMode) {
UICollisionBehaviorModeItems = 1 << 0,元素碰撞
UICollisionBehaviorModeBoundaries = 1 << 1,邊界碰撞
UICollisionBehaviorModeEverything = NSUIntegerMax 全體碰撞
} NS_ENUM_AVAILABLE_IOS(7_0);
//兩個元素相互碰撞
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p;
- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2;
// 視圖碰撞邊界的時候 觸發
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(id <NSCopying>)identifier atPoint:(CGPoint)p;
- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(id <NSCopying>)identifier;
------------------------------------------
3、UIDynamicItemBehavior 動力元素效果
可以設定 動力效果的預設值 是一個輔助的效果 設定運動學元素參與物理效果過程中的參數 如:彈性係數、摩擦係數、密度、阻力、角阻力以及是否允許旋轉等
常用屬性
@property (readwrite, nonatomic) CGFloat elasticity; // Usually between 0 (inelastic) and 1 (collide elastically) 屬性設定碰撞彈性係數 範圍(0.0-1.0)決定了碰撞的彈性程度,比如碰撞時物體的彈性
@property (readwrite, nonatomic) CGFloat friction; // 0 being no friction between objects slide along each other設定摩擦係數 決定了沿接觸面滑動時的摩擦力大小
@property (readwrite, nonatomic) CGFloat density; // 1 by default 密度 跟size相關 計算物體的總品質 品質越大 物體加速或減速就越困難
@property (readwrite, nonatomic) CGFloat resistance; // 0: no velocity damping (阻力):決定線性移動的阻力大小 與摩擦係數不同 摩擦係數只作用於滑動運動
@property (readwrite, nonatomic) CGFloat angularResistance; // 0: no angular velocity damping 設定角度阻力係數。(0--CGFLOAT_MAX)決定旋轉運動時的阻力大小
@property (readwrite, nonatomic) BOOL allowsRotation; // force an item to never rotate 設定行為中的dynamic item是否可以旋轉 設定這個屬性為 NO 物體就完全不會轉動,而無論施加多大的轉動力
--------------------------------
4、 UIPushBehavior :推動效果
typedef NS_ENUM(NSInteger, UIPushBehaviorMode) {
UIPushBehaviorModeContinuous, 持續的力
UIPushBehaviorModeInstantaneous 瞬間的力
} NS_ENUM_AVAILABLE_IOS(7_0);
@property (nonatomic, readonly) UIPushBehaviorMode mode; 推動效果的樣式
@property (nonatomic, readwrite) BOOL active; 是否啟用
@property (readwrite, nonatomic) CGFloat angle; 推動角度
// A continuous force vector with a magnitude of 1.0, applied to a 100 point x 100 point view whose density value is 1.0, results in view acceleration of 100 points per s^2
@property (readwrite, nonatomic) CGFloat magnitude; 推動力量
@property (readwrite, nonatomic) CGVector pushDirection; 推動的方向
------------------------------
5、 UISnapBehavior:迅速移動效果
// The point argument is expressed in the reference coordinate system
- (instancetype)initWithItem:(id <UIDynamicItem>)item snapToPoint:(CGPoint)point;
迅速移動效果 只能一次 添加到一個元素上 snapToPoint 讓他移動到哪一個點
@property (nonatomic, assign) CGFloat damping; // damping value from 0.0 to 1.0. 0.0 is the least oscillation. damping 的範圍是(0.0-1.0)
------------------------------
6、 UIAttachmentBehavior:附著效果
吸附著一個視圖 或者一個點 (也可以多個串連)
附著效果 一個視圖與一個錨點或者另一個視圖相串連的情況
附著行為描述的是兩點之間的串連情況,可以類比剛性或者彈性串連
在多個物體間設定多個UIAttachmentBehavior,可以類比多物體串連
typedef NS_ENUM(NSInteger, UIAttachmentBehaviorType) {
UIAttachmentBehaviorTypeItems, 吸附一個元素
UIAttachmentBehaviorTypeAnchor 吸附一個點
} NS_ENUM_AVAILABLE_IOS(7_0);
設定吸附效果的樣式
@property (readonly, nonatomic) UIAttachmentBehaviorType attachedBehaviorType;
@property (readwrite, nonatomic) CGPoint anchorPoint;錨點
@property (readwrite, nonatomic) CGFloat length;距離 與錨點的距離
@property (readwrite, nonatomic) CGFloat damping; // 1: critical damping 跳躍度
@property (readwrite, nonatomic) CGFloat frequency; // in Hertz 幅度
UIDynamic 基礎認識