UIDynamicAnimator
可以通過該類添加不同的行為,來實現一些動態效果。
初始化方法
// the behaviors (and their dynamic items) that you add to the animator employ the reference view’s coordinate system.
- (instancetype)initWithReferenceView:(UIView *)view
添加到animator上的行為使用reference view的坐標系統。
添加和移除行為
- (void)addBehavior:(UIDynamicBehavior *)behavior;//添加指定的行為動畫
- (void)removeBehavior:(UIDynamicBehavior *)behavior;//移除指定的行為動畫
- (void)removeAllBehaviors;//移除所有的行為
其他屬性
- (NSTimeInterval)elapsedTime;
行為執行的時間,需要注意的是如果我們對同一個行為進行添加和移除操作時,時間會累積。
@property (nonatomic, readonly, getter = isRunning) BOOL running;
判定animator中是否還有行為正在執行
@property (nullable, nonatomic, weak) id <UIDynamicAnimatorDelegate> delegate;
代理協議的實現
- (void)dynamicAnimatorWillResume:(UIDynamicAnimator *)animator;//add行為時調用
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator;//remove行為時調用
UIDynamicItem
說明一下,在很多方法里注意到有很多UIDynamicItem參數的存在。如
- (void)updateItemUsingCurrentState:(id <UIDynamicItem>)item;
其實是說item是實現了UIDynamicItem協議的類,我們發現 UIView(所有視圖控件的父類)實現了該協議。一般情況下,在遇到UIDynamicItem時,直接傳我們的視圖就行了。
UISnapBehavior
將物體通過動畫吸附到一個點上
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
[self.view addGestureRecognizer:tap];
- (void)tapGesture:(UITapGestureRecognizer *)gesture
{
CGPoint tapPoint = [gesture locationInView:self.view];
if (_snapBehavior) {
[self.animator removeBehavior:_snapBehavior];
_snapBehavior = nil;
}
_snapBehavior = [[UISnapBehavior alloc]initWithItem:self.imageView1 snapToPoint:tapPoint];
_snapBehavior.action = ^(){
NSLog(@"UISnapBehavior 在執行");
};
_snapBehavior.damping = 0.9;
[self.animator addBehavior:_snapBehavior];
}
通過觀察action和協議方法的實現,發現物體到達指定點之后會停止行為。
UIGravityBehavior
給物體添加一個類似于自由落體的運動
_gravityBehavior = [[UIGravityBehavior alloc]initWithItems:@[_imageView1]];
_gravityBehavior.gravityDirection = CGVectorMake(0.0f, 1.0f);
[self.animator addBehavior:_gravityBehavior];
gravityDirection
運動的方向,默認是(0.0f, 1.0f)。dx為-1.0f時向左運動,dy為-1.0時向上運動,所以根據0.0~1.0可以定位所有的方向。
angle
也是設置運動的方向,是根據角度來計算的。坐標系為左側為x的正方向,下側為y的正方向。可使用0~M_PI*2之間的值來定位所有方向。
magnitude
可以理解為重力加速度,默認值為1.0。可取負值。
UICollisionBehavior
碰撞檢測,和上面的重力配合查看效果
if (!_gravityBehavior) {
_gravityBehavior = [[UIGravityBehavior alloc]initWithItems:@[_imageView1]];
}
_gravityBehavior = [[UIGravityBehavior alloc]initWithItems:@[_imageView1]];
_gravityBehavior.angle = M_PI_2;
_gravityBehavior.magnitude = 2.0f;
// _gravityBehavior.gravityDirection = CGVectorMake(1.0f, 0.0f);
NSLog(@"magni = %f",_gravityBehavior.magnitude);
if(!_collisionBehavior){
_collisionBehavior = [[UICollisionBehavior alloc]initWithItems:@[_imageView1,_imageView2]];
}
// _collisionBehavior.collisionMode = UICollisionBehaviorModeBoundaries;
_collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:_gravityBehavior];
[self.animator addBehavior:_collisionBehavior];
translatesReferenceBoundsIntoBoundary默認是NO。
另外,UICollisionBehavior有它自己的代理
- (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;
// The identifier of a boundary created with translatesReferenceBoundsIntoBoundary or setTranslatesReferenceBoundsIntoBoundaryWithInsets is nil
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p;
- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier;
UIPushBehavior
給物體一個力的作用
typedef NS_ENUM(NSInteger, UIPushBehaviorMode) {
UIPushBehaviorModeContinuous,//持續的力
UIPushBehaviorModeInstantaneous//一次性的力
}
使用代碼:
if (!_pushBehavior) {
_pushBehavior = [[UIPushBehavior alloc]initWithItems:@[_imageView1] mode:UIPushBehaviorModeContinuous];
}
_pushBehavior.active = YES;
_pushBehavior.pushDirection = CGVectorMake(10.0f, 10.0f);
_pushBehavior.magnitude = 1.0f;
[self.animator addBehavior:_pushBehavior];
pushDirection
給了物體一個運動的方向和初速度
magnitude
給物體一個加速度,默認值為0.0f
active
默認值是YES,將其設置為NO,可以將行為停止。
UIAttachmentBehavior
實現一個效果讓物體跟隨手指移動
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:self.view];
CGPoint imageLocation = [gesture locationInView:self.imageView1];
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
{
NSLog(@"touch position %@",NSStringFromCGPoint(location));
NSLog(@"loction in image %@",NSStringFromCGPoint(imageLocation));
[self.animator removeAllBehaviors];
UIOffset centerOffset = UIOffsetMake(imageLocation.x - CGRectGetMidX(self.imageView1.bounds), imageLocation.y - CGRectGetMidY(self.imageView1.bounds));
_attachmentBehavior = [[UIAttachmentBehavior alloc]initWithItem:self.imageView1 offsetFromCenter:centerOffset attachedToAnchor:location];
_attachmentBehavior.damping = 0.5;
_attachmentBehavior.frequency = 0.8;
[self.animator addBehavior:_attachmentBehavior];
}
break;
case UIGestureRecognizerStateEnded:
{
[self.animator removeBehavior:_attachmentBehavior];
}
break;
default:
{
[_attachmentBehavior setAnchorPoint:[gesture locationInView:self.view]];
}
break;
}
}