iOS UIDynamic物理仿真行为代码示例

UIGravityBehavior重力行为



- (void)viewDidLoad {

    [superviewDidLoad];

    

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

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

//

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

}


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

    //1 创建物理仿真器

    

    //coordinate system 坐标系

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

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

    self.animator = animator;

    

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

    UIGravityBehavior *gravity = [[UIGravityBehavioralloc] 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) IBOutletUIView *myView;

@property (weak, nonatomic) IBOutletUIView *otherView;


@property (nonatomic,strong) UIDynamicAnimator *animator;

@end


//演示1碰撞边界

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

//演示3两个物体碰撞

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

//演示5 碰撞的模式  collisionMode

//演示6检测碰撞


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

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

}

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

    //1 创建物理仿真器

    self.animator = [[UIDynamicAnimatoralloc] initWithReferenceView:self.view];

    

    

    //2 创建仿真行为

    //2.1 重力行为

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

    //2.2 碰撞行为

    UICollisionBehavior *collision = [[UICollisionBehavioralloc] 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 = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(200,300, 200, 150)];

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

    

    

    //检测碰撞

    collision.collisionDelegate = self;

    

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

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

    

    //弹性系数

    item.elasticity = 1;

    //阻力

    item.resistance = 0;

    

    //是否允许物体旋转

//    item.allowsRotation = NO;

    

    

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

    [self.animatoraddBehavior:gravity];

    [self.animatoraddBehavior:collision];

    [self.animatoraddBehavior: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;

        [UIViewanimateWithDuration:0.3animations:^{

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

        } completion:^(BOOL finished) {

            //还原原来的颜色

            view.backgroundColor = color;

        }];

    }

}

@end

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

UISnapBehavior捕捉行为



#import "ViewController.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutletUIView *myView;

@property (nonatomic,strong) UIDynamicAnimator *animator;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // 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 = [[UIDynamicAnimatoralloc] initWithReferenceView:self.view];

    

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

    UISnapBehavior *snap = [[UISnapBehavioralloc] initWithItem:self.myViewsnapToPoint:point];

    

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

    //阻尼系数--阻力

    //默认值是0.5

    snap.damping = 0.5;

    

    [self.animatoraddBehavior:snap];

    

}


@end


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

UIAttachmentBehavior附着行为



#import "ViewController.h"

#import "CZView.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutletUIView *myView;

@property (nonatomic,strong) UIDynamicAnimator *animator;

@end


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

//演示2 连线

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

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


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // 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 = [[UIDynamicAnimatoralloc] initWithReferenceView:self.view];

    //2 创建仿真行为

    //2.1 重力行为

    UIGravityBehavior *gravity = [[UIGravityBehavioralloc] 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;

    [selfsetNeedsDisplay]; //重绘

}


//画线

- (void)drawRect:(CGRect)rect {

    // Drawing code

    UIBezierPath *path = [UIBezierPathbezierPath];

    [path moveToPoint:self.startPoint];

    [path addLineToPoint:self.endPoint];

    [path stroke];

}


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

UIPushBehavior推动行为


#import "ViewController.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutletUIView *myView;

@property (nonatomic,strong) UIDynamicAnimator *animator;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // 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 = [[UIDynamicAnimatoralloc] initWithReferenceView:self.view];

    //2 创建物理仿真行为

    

    //2.1 推行为

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

//    UIPushBehaviorModeInstantaneous  一次性推  只推一次

    UIPushBehavior *push = [[UIPushBehavioralloc] 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.magnitudesqrtf(x * x + y*y) * 0.01;

    

    //2.2 碰撞行为

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

    //设置碰撞的边界

    collision.translatesReferenceBoundsIntoBoundary =YES;

    

    //2.3 重力行为

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

    

    //2.4 辅助项行为

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

    

    //弹性

//    item.elasticity = 1;

    //摩擦力 默认是0

//    item.friction = 50;

    //阻力

//    item.resistance = 1;

    

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

    [self.animatoraddBehavior:push];

    [self.animatoraddBehavior:collision];

//    [self.animator addBehavior:gravity];

    [self.animatoraddBehavior:item];

}


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值