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
#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
相关文章推荐
- [Immutable.js] Exploring Sequences and Range() in Immutable.js
- andriod studio 在open项目时卡在Building gradle project info的解决方案
- Cannot complete the install because one or more required items could not be found
- PowerBuilder115 安装过程
- UICollectionView - 调用delegate定义item大小后不调用collectionView:cellForItemAtIndexPath:函数的解决方法
- strtusAction中的获取request
- 解决“Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误
- Android UI效果之绘图篇(二):Canvas
- hdu 2610 Sequence one【搜索 dfs+剪枝】
- UI视图
- SOUI更新到2.0
- UIPushBehavior的简单使用
- PAT--1085 Perfect Sequence(二分查找)
- 状态栏(UITabBar)、导航栏(UINavigation)
- properties文件的存取与Map键值对排序【按照value进行排序】
- UIPageController的使用
- UI基础UIButton
- NGUI之OnDrag
- UIKit动力学---毛毛虫案例
- 场景NGUI动态字体消失快速显现的临时解决办法