UIDynamic动画效果总结

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  delegate;

代理协议的实现

- (void)dynamicAnimatorWillResume:(UIDynamicAnimator *)animator;//add行为时调用
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator;//remove行为时调用
UIDynamicItem

说明一下,在很多方法里注意到有很多UIDynamicItem参数的存在。如
- (void)updateItemUsingCurrentState:(id )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 )item1 withItem:(id )item2 atPoint:(CGPoint)p;
- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id )item1 withItem:(id )item2;

// The identifier of a boundary created with translatesReferenceBoundsIntoBoundary or setTranslatesReferenceBoundsIntoBoundaryWithInsets is nil
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id )item withBoundaryIdentifier:(nullable id )identifier atPoint:(CGPoint)p;
- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id )item withBoundaryIdentifier:(nullable id )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;
}
}

你可能感兴趣的:(UIDynamic动画效果总结)