什么是UIDynamic?
UIDynamic是從iOS 7開始引入的一種新技術,隸屬于UIKit框架
可以認為是一種物理引擎,能模擬和仿真現實生活中的物理現象
重力、彈性碰撞等現象
UIDynamic使用步驟
- 創建一個物理仿真器(順便設置仿真范圍)
- 創建相應的物理仿真行為(順便添加物理仿真元素)
- 將物理仿真行為添加到物理仿真器中,開始仿真
物理仿真元素
不是任何對象都能做物理仿真元素,不是任何對象都能進行物理仿真
哪些對象才能做物理仿真元素?
任何遵守了UIDynamicItem協議的對象
UIView默認已經遵守了UIDynamicItem協議,因此任何UI控件都能做物理仿真
UICollectionViewLayoutAttributes類默認也遵守UIDynamicItem協議
物理仿真行為
- UIDynamic提供了以下幾種物理仿真行為
- UIGravityBehavior:重力行為
- UICollisionBehavior:碰撞行為
- UISnapBehavior:捕捉行為
- UIPushBehavior:推動行為
- UIAttachmentBehavior:附著行為
- UIDynamicItemBehavior:動力元素行為
物理仿真器
它可以讓物理仿真元素執行物理仿真行為,它是UIDynamicAnimator類型的對象
UIDynamicAnimator的初始化
- (instancetype)initWithReferenceView:(UIView *)view;
view參數:是一個參照視圖,表示物理仿真的范圍
UIDynamicAnimator的常見方法
//添加1個物理仿真行為
- (void)addBehavior:(UIDynamicBehavior *)behavior;
//移除1個物理仿真行為
- (void)removeBehavior:(UIDynamicBehavior *)behavior;
//移除之前添加過的所有物理仿真行為
- (void)removeAllBehaviors;
UIDynamicAnimator的常見屬性
//參照視圖
@property (nonatomic, readonly) UIView* referenceView;
//添加到物理仿真器中的所有物理仿真行為
@property (nonatomic, readonly, copy) NSArray* behaviors;
//是否正在進行物理仿真
@property (nonatomic, readonly, getter = isRunning) BOOL running;
//代理對象(能監聽物理仿真器的仿真過程,比如開始和結束)
@property (nonatomic, assign) id <UIDynamicAnimatorDelegate> delegate;
直接上代碼
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIDynamicAnimator *animator;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmented;
@property (weak, nonatomic) IBOutlet UIView *blueView;
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UIView *orangeView;
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
self.blueView.transform = CGAffineTransformMakeRotation(M_PI_4);
self.segmented.transform = CGAffineTransformMakeRotation(-M_PI / 8);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent: (UIEvent *)event {
//創建重力行為
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc]init];
[gravityBehavior addItem:self.blueView];
[gravityBehavior addItem:self.orangeView];
//加速度
gravityBehavior.magnitude = 3;
//創建碰撞行為
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc]init];
//碰撞類型為元素和邊界
collisionBehavior.collisionMode = UICollisionBehaviorModeEverything;
CGFloat Y = self.view.frame.size.height - CGRectGetHeight(self.redView.frame);
CGFloat X = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
//設置紅色的View為底邊界,左邊框跟右邊框作為邊界
[collisionBehavior addBoundaryWithIdentifier:@"collision1" fromPoint:CGPointMake(0,Y) toPoint:CGPointMake(X, Y)];
[collisionBehavior addBoundaryWithIdentifier:@"collision2" fromPoint:CGPointMake(0, 0) toPoint:CGPointMake(0, height)];
[collisionBehavior addBoundaryWithIdentifier:@"collision3" fromPoint:CGPointMake(X,0) toPoint:CGPointMake(X, height)];
[collisionBehavior addItem:self.blueView];
[collisionBehavior addItem:self.segmented];
[collisionBehavior addItem:self.orangeView];
[self.animator addBehavior:collisionBehavior];
[self.animator addBehavior:gravityBehavior];
}
- (UIDynamicAnimator *)animator{
if (_animator == nil) {
_animator = [[UIDynamicAnimator alloc]init];
}
return _animator;
}
@end