您的位置:首页 > 产品设计 > UI/UE

UIGravityBehavior的简单使用

2016-02-22 15:47 393 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: