UIDynamic物理仿真行为代码示例

UIGravityBehavior重力行为



- (void)viewDidLoad {

    [super viewDidLoad];

    

//    UIView  --继承了UIDynamicItem协议---物体

// 父类UIDynamicBehavior  -- 物理仿真行为  iOS7以后才有效

//

//    UIDynamicAnimator  --物理仿真器   --物理仿真行为添加到物理仿真器  开始工作

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //1 创建物理仿真器

    

    //coordinate system 坐标系

    //initWithReferenceView 设置物理仿真器的参考视图, 仿真行为和仿真元素会使用参考视图的坐标系

    UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    self.animator = animator;

    

    //2 创建物理仿真行为  Gravity重力   -- 设置哪些物体实现该行为

    UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.myView]];

    

    //4 设置物理仿真行为的属性

    //设置向量

//    gravity.gravityDirection = CGVectorMake(500, 500);

    

//    //弧度 方向

//    gravity.angle = 0;

//    //重力加速度 100 point / s*s

//    gravity.magnitude = 1;

    

    

    //当动画执行时候会不停调用action

    [gravity setAction:^{

        NSLog(@"%@",NSStringFromCGRect(self.myView.frame));

    }];

    

    

    //3 把物理仿真行为添加到物理仿真器中

    [animator addBehavior:gravity];

}

====================================

UICollisionBehavior碰撞行为

#import "ViewController.h"


@interface ViewController () <UICollisionBehaviorDelegate>

@property (weak, nonatomic) IBOutlet UIView *myView;

@property (weak, nonatomic) IBOutlet UIView *otherView;


@property (nonatomic, strong) UIDynamicAnimator *animator;

@end


//演示1 碰撞边界

//演示2 通过辅助行为--设置弹性系数,阻力系数,是否旋转

//演示3 两个物体碰撞

//演示4 自定义碰撞边界--设置两个点,设置路径

//演示5 碰撞的模式  collisionMode

//演示6 检测碰撞


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //1 创建物理仿真器

    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    

    

    //2 创建仿真行为

    //2.1 重力行为

    UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.myView,self.otherView]];

    //2.2 碰撞行为

    UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.myView,self.otherView]];

    //参考视图的边界作为碰撞的边界

    collision.translatesReferenceBoundsIntoBoundary = YES;

    

    //碰撞的模式

    //UICollisionBehaviorModeItems 只和物体碰撞

    //UICollisionBehaviorModeBoundaries 只和边界碰撞

    //UICollisionBehaviorModeEverything   物体碰撞和边界碰撞

    collision.collisionMode = UICollisionBehaviorModeEverything;

    

    //2.2.1 设置碰撞边界

//    [collision addBoundaryWithIdentifier:@"b1" fromPoint:CGPointMake(0, 400) toPoint:CGPointMake(190, 400)];

     //2.2.2 设置碰撞边界

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(200, 300, 200, 150)];

    [collision addBoundaryWithIdentifier:@"b2" forPath:path];

    

    

    //检测碰撞

    collision.collisionDelegate = self;

    

    //2.3 辅助行为--设置弹性

    UIDynamicItemBehavior *item = [[UIDynamicItemBehavior alloc] initWithItems:@[self.myView]];

    

    //弹性系数

    item.elasticity = 1;

    //阻力

    item.resistance = 0;

    

    //是否允许物体旋转

//    item.allowsRotation = NO;

    

    

    //3 把行为添加到物理仿真器

    [self.animator addBehavior:gravity];

    [self.animator addBehavior:collision];

    [self.animator addBehavior:item];

}



//物体跟边界开始碰撞

- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p {

    

    //碰撞的物体

    UIView *view = (UIView *)item;

    //边界的id

    NSString *strId = (NSString *)identifier;

    

    //判断碰撞的边界id是否是预期的

    if ([strId isEqualToString:@"b2"]) {

        NSLog(@"==");

        //随机颜色

        int random1 = arc4random_uniform(256);

        int random2 = arc4random_uniform(256);

        int random3 = arc4random_uniform(256);


        

        //记录下来原来的颜色

        UIColor *color = view.backgroundColor;

        [UIView animateWithDuration:0.3 animations:^{

            view.backgroundColor = [UIColor colorWithRed:random1/255.0 green:random2/255.0 blue:random3/255.0 alpha:1];

        } completion:^(BOOL finished) {

            //还原原来的颜色

            view.backgroundColor = color;

        }];

    }

}

@end

==================================================

UISnapBehavior捕捉行为



#import "ViewController.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *myView;

@property (nonatomic, strong) UIDynamicAnimator *animator;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //获取当前点击的坐标

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self.view];

    

    //1 创建物理仿真器

    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    

    //2 创建捕捉行为 -- 改变的是物体中心点的坐标

    UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.myView snapToPoint:point];

    

    //3 把行为添加到仿真器

    //阻尼系数--阻力

    //默认值是0.5

    snap.damping = 0.5;

    

    [self.animator addBehavior:snap];

    

}


@end


=========================================

UIAttachmentBehavior附着行为



#import "ViewController.h"

#import "CZView.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *myView;

@property (nonatomic, strong) UIDynamicAnimator *animator;

@end


//演示1 重力行为和附着行为--刚性行为

//演示2 连线

//演示3 弹性行为--把频率设置为非0

//演示4 设置中心点的偏移


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.myView.alpha = 0.5;

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //获取点击的坐标

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self.view];

    

    //1 创建物理仿真器

    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    //2 创建仿真行为

    //2.1 重力行为

    UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.myView]];

    //2.2 附着行为   Attachment附件

    

    //默认是和物体的中心点附着...修改中心点的偏移量 ,附着左上角

    CGSize size = self.myView.bounds.size; //myViewsize

    UIOffset offset = UIOffsetMake(-size.width * 0.5, -size.height * 0.5);

    UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:self.myView offsetFromCenter:offset attachedToAnchor:point];

    //刚性行为

    //弹性行为

    

    //把控制器的view 转换成CZView

    CZView *view = (CZView *)self.view;

    

    //action  当动画执行的时候执行--画线

    [gravity setAction:^{

        //设置起始点和结束点

        view.startPoint = point;

        

        //获取myView坐标系中 myView左上角的点 0,0

        CGPoint myPoint = CGPointZero;

        //myView坐标系中的点,转换成self.view坐标系中的坐标

        view.endPoint = [self.view convertPoint:myPoint fromView:self.myView];

    }];

    

    //长度--设置中心点和附着点之间的距离

//    attachment.length = 60;

    

    

    //弹性行为

    //阻尼 减震  -- 默认值 0

//    attachment.damping = 0.5;

//    //频率  -- 默认值 当震动频率为没有弹性

//    attachment.frequency = 0.5;

    

    

    

    //3 把行为添加到仿真器

    [self.animator addBehavior:gravity];

    [self.animator addBehavior:attachment];

}


@end



#import "CZView.h"


@implementation CZView


- (void)setEndPoint:(CGPoint)endPoint {

    _endPoint = endPoint;

    [self setNeedsDisplay]; //重绘

}


//画线

- (void)drawRect:(CGRect)rect {

    // Drawing code

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:self.startPoint];

    [path addLineToPoint:self.endPoint];

    [path stroke];

}


==========================================================

UIPushBehavior推动行为


#import "ViewController.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *myView;

@property (nonatomic, strong) UIDynamicAnimator *animator;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //获取点击的点

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self.view];  //点击的点的坐标

    

    //1 创建物理仿真器

    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    //2 创建物理仿真行为

    

    //2.1 推行为

//    UIPushBehaviorModeContinuous,  持续推  一直有手推着

//    UIPushBehaviorModeInstantaneous  一次性推  只推一次

    UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[self.myView] mode:UIPushBehaviorModeContinuous];

    

    //加速度--默认是必须设置该值

//    push.magnitude = 1;

//    push.angle = M_PI_2;

    

    //向量

//    push.pushDirection = CGVectorMake(0, 1);

    

    

    //计算推动的方向

    CGFloat x = self.myView.center.x - point.x;

    CGFloat y = self.myView.center.y - point.y;

    push.pushDirection = CGVectorMake(x, y);

    //设置加速度

    push.magnitude sqrtf(x * x + y*y) * 0.01;

    

    //2.2 碰撞行为

    UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.myView]];

    //设置碰撞的边界

    collision.translatesReferenceBoundsIntoBoundary = YES;

    

    //2.3 重力行为

//    UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.myView]];

    

    //2.4 辅助项行为

    UIDynamicItemBehavior *item = [[UIDynamicItemBehavior alloc] initWithItems:@[self.myView]];

    

    //弹性

//    item.elasticity = 1;

    //摩擦力  默认是0

//    item.friction = 50;

    //阻力

//    item.resistance = 1;

    

    //3 把行为添加到仿真器

    [self.animator addBehavior:push];

    [self.animator addBehavior:collision];

//    [self.animator addBehavior:gravity];

    [self.animator addBehavior:item];

}


@end



你可能感兴趣的:(UIDynamic物理仿真行为代码示例)