#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
@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