#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);
//重力的方向 默认是(0,1) 改变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