Dynamic(碰撞动画)

本文详细介绍了如何使用iOS开发中的UIDynamicAnimator来创建动态效果,包括碰撞、重力、弹性等行为,并通过实例展示了具体实现过程。
#import "ViewController.h"

@interface ViewController ()<UICollisionBehaviorDelegate>
{
    UIDynamicAnimator *dynamicAnimator;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self drawPath];
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 0, 100, 100)];
    view1.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view1];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(250, 0, 100, 100)];
    view2.backgroundColor = [UIColor redColor];
    [self.view addSubview:view2];
    
    //创建动画执行者
    dynamicAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    
    //创建重力行为
    UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] init];
    gravityBehavior.magnitude = 1;//重力加速度
    [gravityBehavior addItem:view1];
    [gravityBehavior addItem:view2];
    [dynamicAnimator addBehavior:gravityBehavior];
    
    //碰撞行为
    UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[view1,view2]];
    //将参照物的边界转化为可以碰撞的边界
    collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:[self createPath]];
    path.lineWidth = 50;
    [collisionBehavior addBoundaryWithIdentifier:@"path" forPath:path];
    
    collisionBehavior.collisionDelegate = self;
    [dynamicAnimator addBehavior:collisionBehavior];
    
    UIDynamicItemBehavior *item1 = [[UIDynamicItemBehavior alloc] initWithItems:@[view1]];
    item1.elasticity = 0;//弹性
    item1.density = 10;//密度
    [dynamicAnimator addBehavior:item1];
    
    UIDynamicItemBehavior *item2 = [[UIDynamicItemBehavior alloc] initWithItems:@[view2]];
    item2.elasticity = 1;
//    item2.density = 5;
    [dynamicAnimator addBehavior:item2];
    
    //设置推力
    UIPushBehavior *pushBehavior = [[UIPushBehavior alloc] initWithItems:@[view2] mode:UIPushBehaviorModeContinuous];
    pushBehavior.angle = M_PI_4;
    pushBehavior.magnitude = 10;
    [dynamicAnimator addBehavior:pushBehavior];
    [pushBehavior setActive:YES];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (CGMutablePathRef)createPath
{
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, 0, 100);
    CGPathAddLineToPoint(path, nil, self.view.bounds.size.width, 150);
    
    return path;
}

- (void)drawPath
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = self.view.bounds;
    shapeLayer.path = [self createPath];
    shapeLayer.strokeColor = [UIColor blackColor].CGColor;
    shapeLayer.lineWidth = 10;
    [self.view.layer addSublayer:shapeLayer];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p
//{
//    NSLog(@"%@\n%@", item1, item2);
//}
//
//- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2
//{
//    NSLog(@"%@\n%@", item1, item2);
//}
//
//- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(id <NSCopying>)identifier atPoint:(CGPoint)p
//{
//    //    NSLog(@"%@\n%@", item, identifier);
//    NSLog(@"%f", dynamicAnimator.elapsedTime);
//}
//
//- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(id <NSCopying>)identifier
//{
//    //    NSLog(@"%@\n%@", item, identifier);
//}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值