IOS基础之UIDynamicAnimator动力学入门
01-重力
//
// ViewController.m
// 01-重力
//
// Created by 鲁军 on 2021/4/15.
//
#import "ViewController.h"
@interface ViewController()
@property(nonatomic,weak)UIView *redView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//根据某一个范围 创建动画者对象
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//2根据某一个动力学元素,创建行为
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
//3把行为添加到动画者当中
[self.animator addBehavior:gravity];
// gravity.angle = M_PI; //用角度的形式设置方向
// gravity.gravityDirection = CGVectorMake(1, 1); //右下角跑
// gravity.magnitude = 100;//重力的量级。即重力加速度
}
- (void)viewDidLoad {
[super viewDidLoad];
UIView * redView =[[UIView alloc] init ];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:redView];
self.redView = redView;
}
@end
02-碰撞
//
// ViewController.m
// 02-碰撞
//
// Created by 鲁军 on 2021/4/15.
//
#import "ViewController.h"
@interface ViewController()
@property(nonatomic,weak)UIView *redView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//根据某一个范围 创建动画者对象
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//2根据某一个动力学元素,创建行为
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
// 3碰撞行为
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView]];
//把引用的view 的Bounds 变成边界
collision.translatesReferenceBoundsIntoBoundary = YES;
//4把行为添加到动画者当中
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
}
- (void)viewDidLoad {
[super viewDidLoad];
UIView * redView =[[UIView alloc] init ];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:redView];
self.redView = redView;
}
@end
03-碰撞与另外一个Item碰撞
//
// ViewController.m
// 03-碰撞与另外一个Item碰撞
//
// Created by 鲁军 on 2021/4/17.
//
#import "ViewController.h"
#define H [UIScreen mainScreen].bounds.size.height
@interface ViewController()
@property(nonatomic,weak)UIView *redView;
@property(nonatomic,weak)UIView *blueView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//根据某一个范围 创建动画者对象
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//2根据某一个动力学元素,创建行为
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
// 3碰撞行为
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];
//把引用的view 的Bounds 变成边界
collision.translatesReferenceBoundsIntoBoundary = YES;
//4把行为添加到动画者当中
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
}
- (void)viewDidLoad {
[super viewDidLoad];
UIView * redView =[[UIView alloc] init ];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:redView];
self.redView = redView;
UIView * blueView =[[UIView alloc] init ];
blueView.backgroundColor = [UIColor blueColor];
blueView.frame = CGRectMake(170,H - 50, 50, 50);
[self.view addSubview:blueView];
self.blueView = blueView;
}
@end
04-碰撞模式
//
// ViewController.m
// 04-碰撞模式
//
// Created by 鲁军 on 2021/4/17.
//
#import "ViewController.h"
#define H [UIScreen mainScreen].bounds.size.height
@interface ViewController()
@property(nonatomic,weak)UIView *redView;
@property(nonatomic,weak)UIView *blueView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//根据某一个范围 创建动画者对象
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//2根据某一个动力学元素,创建行为
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
// 3碰撞行为
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];
//把引用的view 的Bounds 变成边界
collision.translatesReferenceBoundsIntoBoundary = YES;
//碰撞模式
//UICollisionBehaviorModeItems 仅仅Item 和 Item 之间发生的碰撞
//UICollisionBehaviorModeBoundaries 仅仅是和边界发生碰撞
//UICollisionBehaviorModeEverything Item 和边界都发生碰撞 默认是everything
collision.collisionMode = UICollisionBehaviorModeEverything;
//4把行为添加到动画者当中
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
}
- (void)viewDidLoad {
[super viewDidLoad];
UIView * redView =[[UIView alloc] init ];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:redView];
self.redView = redView;
UIView * blueView =[[UIView alloc] init ];
blueView.backgroundColor = [UIColor blueColor];
blueView.frame = CGRectMake(170,H - 50, 50, 50);
[self.view addSubview:blueView];
self.blueView = blueView;
}
@end
05- 碰撞行为-创建边界
//
// ViewController.m
// 05-碰撞行为-创建边界
//
// Created by 鲁军 on 2021/4/17.
//
#import "ViewController.h"
#define H [UIScreen mainScreen].bounds.size.height
@interface BGView : UIView
@end
@implementation BGView
- (void)drawRect:(CGRect)rect{
//CGPointMake(0, 200) toPoint:CGPointMake(150, 250)];
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(0, 200)];
[path addLineToPoint:CGPointMake(150, 250)];
[path stroke];
}
@end
@interface ViewController()
@property(nonatomic,weak)UIView *redView;
@property(nonatomic,weak)UIView *blueView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end
@implementation ViewController
//优先级是最高的
- (void)loadView{
self.view = [[BGView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//根据某一个范围 创建动画者对象
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//2根据某一个动力学元素,创建行为
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
// 3碰撞行为
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];
//把引用的view 的Bounds 变成边界
collision.translatesReferenceBoundsIntoBoundary = YES;
//添加边界
//以一条线为边界
// [collision addBoundaryWithIdentifier:@"key" fromPoint:CGPointMake(0, 200) toPoint:CGPointMake(150, 250)];
//以一个自定义的矩形框 为边界
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.blueView.frame];
[collision addBoundaryWithIdentifier:@"key" forPath:path];
//4把行为添加到动画者当中
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
}
- (void)viewDidLoad {
[super viewDidLoad];
UIView * redView =[[UIView alloc] init ];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:redView];
self.redView = redView;
UIView * blueView =[[UIView alloc] init ];
blueView.backgroundColor = [UIColor blueColor];
blueView.frame = CGRectMake(170,H - 150, 50, 50);
[self.view addSubview:blueView];
self.blueView = blueView;
}
@end
06-碰撞行为-action
//
// ViewController.m
// 06-碰撞行为-action
//
// Created by 鲁军 on 2021/4/17.
//
#import "ViewController.h"
#define H [UIScreen mainScreen].bounds.size.height
@interface BGView : UIView
@property(nonatomic,assign)CGRect redRect;
@end
@implementation BGView
- (void)drawRect:(CGRect)rect{
//CGPointMake(0, 200) toPoint:CGPointMake(150, 250)];
/* UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(0, 200)];
[path addLineToPoint:CGPointMake(150, 250)];
[path stroke];*/
[[UIBezierPath bezierPathWithRect:self.redRect] stroke];
}
@end
@interface ViewController()
@property(nonatomic,weak)UIView *redView;
@property(nonatomic,weak)UIView *blueView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end
@implementation ViewController
//优先级是最高的
- (void)loadView{
self.view = [[BGView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//根据某一个范围 创建动画者对象
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//2根据某一个动力学元素,创建行为
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
// 3碰撞行为
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];
//把引用的view 的Bounds 变成边界
collision.translatesReferenceBoundsIntoBoundary = YES;
//添加边界
//以一条线为边界
// [collision addBoundaryWithIdentifier:@"key" fromPoint:CGPointMake(0, 200) toPoint:CGPointMake(150, 250)];
//以一个自定义的矩形框 为边界
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.blueView.frame];
[collision addBoundaryWithIdentifier:@"key" forPath:path];
collision.action = ^{
NSLog(@"%@",NSStringFromCGRect(self.redView.frame));
BGView *bgView = (BGView *)self.view;
bgView.redRect = self.redView.frame;
[self.view setNeedsDisplay]; //重绘
if(self.redView.frame.size.width > 105){
self.redView.backgroundColor = [UIColor yellowColor];
}else {
self.redView.backgroundColor = [UIColor redColor];
}
};
//4把行为添加到动画者当中
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
}
- (void)viewDidLoad {
[super viewDidLoad];
UIView * redView =[[UIView alloc] init ];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:redView];
self.redView = redView;
UIView * blueView =[[UIView alloc] init ];
blueView.backgroundColor = [UIColor blueColor];
blueView.frame = CGRectMake(170,H - 150, 50, 50);
[self.view addSubview:blueView];
self.blueView = blueView;
}
@end
07-碰撞的行为-代理方法
//
// ViewController.m
// 07-碰撞的行为-代理方法
//
// Created by 鲁军 on 2021/4/17.
//
#import "ViewController.h"
#define H [UIScreen mainScreen].bounds.size.height
@interface BGView : UIView
@property(nonatomic,assign)CGRect redRect;
@end
@implementation BGView
- (void)drawRect:(CGRect)rect{
//CGPointMake(0, 200) toPoint:CGPointMake(150, 250)];
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(0, 200)];
[path addLineToPoint:CGPointMake(150, 250)];
[path stroke];
[[UIBezierPath bezierPathWithRect:self.redRect] stroke];
}
@end
@interface ViewController() <UICollisionBehaviorDelegate>
@property(nonatomic,weak)UIView *redView;
@property(nonatomic,weak)UIView *blueView;
@property(nonatomic,strong)UIDynamicAnimator *animator;//使用强引用,不会立刻消失
@end
@implementation ViewController
//优先级是最高的
- (void)loadView{
self.view = [[BGView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//根据某一个范围 创建动画者对象
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//2根据某一个动力学元素,创建行为
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
// 3碰撞行为
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];
//把引用的view 的Bounds 变成边界
collision.translatesReferenceBoundsIntoBoundary = YES;
//添加边界
//以一条线为边界
[collision addBoundaryWithIdentifier:@"key1" fromPoint:CGPointMake(0, 200) toPoint:CGPointMake(150, 250)];
//以一个自定义的矩形框 为边界
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.blueView.frame];
[collision addBoundaryWithIdentifier:@"key2" forPath:path];
collision.action = ^{
// NSLog(@"%@",NSStringFromCGRect(self.redView.frame));
BGView *bgView = (BGView *)self.view;
bgView.redRect = self.redView.frame;
[self.view setNeedsDisplay]; //重绘
};
//设置代理
collision.collisionDelegate = self;
//4把行为添加到动画者当中
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
}
- (void)viewDidLoad {
[super viewDidLoad];
UIView * redView =[[UIView alloc] init ];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:redView];
self.redView = redView;
UIView * blueView =[[UIView alloc] init ];
blueView.backgroundColor = [UIColor blueColor];
blueView.frame = CGRectMake(170,H - 150, 50, 50);
[self.view addSubview:blueView];
self.blueView = blueView;
}
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item1 withItem:(id<UIDynamicItem>)item2 atPoint:(CGPoint)p{
}
//代理方法
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p{
//强制转化
NSString *str = (NSString *) identifier;
if( [str isEqualToString:@"key1"]){
self.redView.backgroundColor = [UIColor orangeColor];
}else{
self.redView.backgroundColor =[UIColor redColor];
}
}
@end
08-甩行为
//
// ViewController.m
// 08-甩行为
//
// Created by 鲁军 on 2021/4/17.
//
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,weak)UIView *redView;
@property(nonatomic,strong)UIDynamicAnimator *animator;
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//获取触摸对象
UITouch *t = touches.anyObject;
CGPoint p = [t locationInView:t.view];
//1动画者对象
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//2创建行为
UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:p];
//damping 阻尼 值越小 晃动越大 0 - 2。 减速 属性
snap.damping = 2;
//3把行为添加到动画者当中
[self.animator addBehavior:snap];
}
- (void)viewDidLoad {
[super viewDidLoad];
UIView * redView =[[UIView alloc] init ];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:redView];
self.redView = redView;
}
@end
09-附着行为
//
// ViewController.m
// 09-附着行为
//
// Created by 鲁军 on 2021/4/17.
//
#import "ViewController.h"
@interface BGView : UIView
@property(nonatomic,assign)CGPoint startPoint;
@property(nonatomic,assign)CGPoint endPoint;
@end
@implementation BGView
- (void)drawRect:(CGRect)rect{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:self.startPoint];
[path addLineToPoint:self.endPoint];
[path stroke];
}
@end
@interface ViewController ()
@property(nonatomic,weak)UIView *redView;
@property(nonatomic,strong)UIDynamicAnimator *animator;
@property(nonatomic,strong)UIAttachmentBehavior *attach;
@end
@implementation ViewController
- (void)loadView{
self.view = [[BGView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//获取触摸对象
UITouch *t = touches.anyObject;
CGPoint p = [t locationInView:t.view];
//1动画者对象
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//2创建行为
//刚性附着
self.attach = [[UIAttachmentBehavior alloc] initWithItem:self.redView attachedToAnchor:p];
self.attach.length = 100;
UIGravityBehavior * gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
//3把行为添加到动画者当中
[self.animator addBehavior:gravity];
[self.animator addBehavior:self.attach];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//获取触摸对象
UITouch *t = touches.anyObject;
CGPoint p = [t locationInView:t.view];
self.attach.anchorPoint = p;
__weak ViewController *weakView = self;
self.attach.action= ^{
BGView *bgView = (BGView *) weakView.view;
bgView.startPoint = weakView.redView.center;
bgView.endPoint = p;
[weakView.view setNeedsDisplay];
};
}
- (void)viewDidLoad {
[super viewDidLoad];
UIView * redView =[[UIView alloc] init ];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:redView];
self.redView = redView;
self.redView.alpha = 0.5;
}
@end