iOS UIDynamic简介 —— HERO博客

UIDynamic简介:

UIDynamic是从iOS 7开始引入的一种技术,是UIKit提供的非常方便的API,类似物理引擎效果,可以模仿重力、碰撞、反弹等效果。

UIDynamic包含几个主要属性:

1. 行为:

UIGravityBehavior:重力行为

UICollisionBehavior:碰撞行为

UISnapBehavior:捕捉行为

UIPushBehavior:推动行为

UIAttachmentBehavior:附着行为

UIDynamicItemBehavior:动力元素行为

2. 对象:

遵守了UIDynamicItem协议的对象,如UIView、UICollectionViewLayoutAttributes类默认已经遵守了UIDynamicItem协议,因此任何UI控件都能做物理仿真。

3.物理仿真器:

让对象执行仿真行为。

UIDynamic使用:

写了个小列子,简单说明一下UIDynamic的使用,先看一下效果图:


下面贴上代码:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

#import "ViewController.h"

#define mainW [UIScreen mainScreen].bounds.size.width
#define mainH [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@property (nonatomic, strong) UIDynamicAnimator *animator;
@property (nonatomic, weak) UIView *redView;
@property (nonatomic, weak) UIView *yellowView;
@property (nonatomic, weak) UIView *greenView;
@property (nonatomic, weak) UIView *blueView;

@end

@implementation ViewController

- (UIDynamicAnimator *)animator
{
    if (_animator == nil) {
        //创建物理仿真器
        _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    }
    return _animator;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    //创建控件
    [self creatControl];
}

- (void)creatControl
{
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake((mainW - 51) * 0.5, mainH - 51, 51, 51)];
    for (int i = 0; i < 51; i++) {
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(26 - i, i, 2 * i + 1, 1)];
        line.backgroundColor = [UIColor blueColor];
        [blueView addSubview:line];
    }
    [self.view addSubview:blueView];
    self.blueView = blueView;
    
    UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake((mainW - 200) * 0.5, CGRectGetMinY(blueView.frame) - 20, 200, 20)];
    yellowView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:yellowView];
    self.yellowView = yellowView;
    
    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(yellowView.frame), CGRectGetMinY(yellowView.frame) - 40, 40, 40)];
    greenView.backgroundColor = [UIColor greenColor];
    greenView.layer.cornerRadius = 20;
    greenView.clipsToBounds = YES;
    [self.view addSubview:greenView];
    self.greenView = greenView;
    
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(yellowView.frame) - 50, CGRectGetMinY(yellowView.frame) - 50, 50, 50)];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    self.redView = redView;
    
    UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake((mainW - 100) * 0.5, 50, 100, 30)];
    resetBtn.backgroundColor = [UIColor orangeColor];
    [resetBtn setTitle:@"重置" forState:UIControlStateNormal];
    [resetBtn addTarget:self action:@selector(resetBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:resetBtn];
}

- (void)resetBtnOnClick
{
    for (UIControl *control in self.view.subviews) {
        [control removeFromSuperview];
    }
    [self creatControl];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //获取一个触摸点
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    //创建捕捉行为
    UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:point];
    //设置防震系数(0~1,数值越大,震动的幅度越小)
    snap.damping = 0.5;
    //删除之前的所有仿真行为
    [self.animator removeAllBehaviors];
    //执行捕捉行为
    [self.animator addBehavior:snap];
    //设置0.5秒延时执行
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //重力碰撞
        [self GravityCollisionStart];
    });
}

-(void)GravityCollisionStart
{
    //重力行为
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init];
    //设置重力的加速度,重力的加速度越大,碰撞就越厉害
    gravity.magnitude = 10;
    //设置重力的方向(水平,垂直)
    gravity.gravityDirection = CGVectorMake(0, 10);
    [gravity addItem:self.redView];
    //碰撞行为
    UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
    [collision addItem:self.redView];
    [collision addItem:self.yellowView];
    [collision addItem:self.greenView];
    [collision addItem:self.blueView];
    //让参照视图的边框成为碰撞检测的边界
    collision.translatesReferenceBoundsIntoBoundary = YES;
    //执行仿真
    [self.animator removeAllBehaviors];
    [self.animator addBehavior:gravity];
    [self.animator addBehavior:collision];
}

@end


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值