UIGravityBehavior的简单使用


#import "ViewController.h"

#import "PushView.h"


@interface ViewController ()


@property (weak, nonatomic) UIImageView * imageView1;


@property (strong, nonatomic) UIDynamicAnimator * animator;


@end


@implementation ViewController


- (void)viewDidLoad {

  

  [super viewDidLoad];

  

  [self setupViews];

}

- (void)setupViews{

  

  UIImageView * imageView1 = [[UIImageView alloc]init];

  imageView1.backgroundColor = [UIColor redColor];

  imageView1.frame = CGRectMake(50, 100, 100, 100);

  imageView1.layer.masksToBounds = YES;

  imageView1.layer.cornerRadius = imageView1.frame.size.width/2;

  imageView1.userInteractionEnabled = YES;

  _imageView1 = imageView1;

  [self.view addSubview:imageView1];

}


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

  

  CGPoint point = [touches.anyObject locationInView:self.view];

  self.imageView1.center = point;

  

  [self.animator removeAllBehaviors];

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

  

  //默认是1.570796

  NSLog(@"angle %f",gravity.angle);

  

  //默认是1.0

  gravity.magnitude = 10.0;

  

  NSLog(@"magnitude %f",gravity.magnitude);

  //重力的方向 默认是(01 改变angle 或者magnitude 也能改变这个值

  gravity.gravityDirection = CGVectorMake(0, 1);

  

  [self.animator addBehavior:gravity];


  //加一个UICollisionBehavior 就不会出现在屏幕外面

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

  collision.translatesReferenceBoundsIntoBoundary = YES;

  [self.animator addBehavior:collision];

  

}

- (UIDynamicAnimator *)animator{

  

  if (_animator == nil) {

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

  }

  return _animator;

  

}

@end


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值