美文网首页iOS动画IOS程序员
Day.03.12 UIGravityBehavior 重力作用

Day.03.12 UIGravityBehavior 重力作用

作者: 挂树上的骷髅怪 | 来源:发表于2016-03-12 16:37 被阅读150次

20W以下的项目用不到,20W以上的项目用一点
ViewController.m

#import "ViewController.h"

#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()
{
    UIDynamicAnimator *_animator;
    UIView *_viewI;
    UIView *_viewII;
}
@end

@implementation ViewController
- (IBAction)gravity:(UIButton *)sender {
    
    /*——————————————————————————————————————————————————————————————————————————————-*/
    
    //2⃣️创建动力学行为 --> 并绑定item(动力学元素)
    
    //重力行为
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[_viewI]];
    
    //3⃣️仿真器添加重力行为
    [_animator addBehavior:gravity];
    
    //碰撞检测行为
    UICollisionBehavior *collision = [[UICollisionBehavior alloc]initWithItems:@[_viewI,_viewII]];
    
    //开启参考边界
    collision.translatesReferenceBoundsIntoBoundary = YES;
    
    //仿真器添加碰撞行为
    [_animator addBehavior:collision];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1⃣️实例化仿真器 referenceView:参考系
    _animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    
    //创建viewI
    _viewI = [[UIView alloc]initWithFrame:CGRectMake(KScreenWidth/2, 20, 50, 50)];
    
    _viewI.backgroundColor = [UIColor blackColor];
    
    _viewI.transform = CGAffineTransformMakeRotation(M_PI_4);
    
    [self.view addSubview:_viewI];
    
    //创建viewII
    _viewII = [[UIView alloc]initWithFrame:CGRectMake(0, KScreenHeight/2, KScreenWidth*2/3, 10)];
    
    _viewII.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:_viewII];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

屏幕快照 2016-03-12 下午4.31.41.png 屏幕快照 2016-03-12 下午4.31.00.png

相关文章

网友评论

    本文标题:Day.03.12 UIGravityBehavior 重力作用

    本文链接:https://www.haomeiwen.com/subject/szpxlttx.html