UIDynamic动画效果总结

作者: 懒得起名的伊凡 | 来源:发表于2015-10-26 17:40 被阅读3012次

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

相关文章

  • UIDynamic动画效果总结

    UIDynamicAnimator 可以通过该类添加不同的行为,来实现一些动态效果。 初始化方法 添加到anima...

  • UIDynamic动画

    基础知识 UIDynamic是物理仿真动画,使用前先了解三个类:物理仿真元素(Dynamic Item):要执行动...

  • iOS动画三板斧(三)--UIDynamic动画

    终于到了动画三板斧第三篇了,这里用UIDynamic来实现动画。UIDynamic是iOS 7之后新添加的一些物理...

  • UIDynamic

    简介 UIDynamic是iOS 7之后新添加的一些物理仿真动画库,包含在UIKit框架中。 UIDynamic中...

  • UIDynamic动画(swift)

    本文简单介绍了UIDynamic的以下三种物理仿真行为 UIGravityBehavior:重力行为 UIColl...

  • Siri 效果实现外加UIDynamic有趣动画

  • 动画效果总结

    只是简单记录一下,动画的实现很简单,步骤不多,在Android 3.0版本之前,我们使用的是逐帧动画(frame-...

  • UIDynamic动画(很好玩)

    最近公司的项目有点紧,这段时间抽时间整理下关于UIDynamic的思路,现在将牛顿摆锤动画做一个简单的实现. 根据...

  • iOS UIDynamic物理效果

    各种碰撞、吸附、点击展开等物理视觉效果大都是通过UIDynamic来实现的 直接上代码看各种效果:

  • iOS开发拓展篇—UIDynamic(简单介绍)

    iOS开发拓展篇—UIDynamic(简单介绍) 一、简单介绍 1.什么是UIDynamic UIDynamic是...

网友评论

  • 573e064f4246:楼主,有没有一些关于 collectionView 的物理效果layout的文章 :grin:

本文标题:UIDynamic动画效果总结

本文链接:https://www.haomeiwen.com/subject/jkrxhttx.html