UIPushBehavior的简单使用
来源:互联网 发布:ubuntu kylin iso下载 编辑:程序博客网 时间:2024/06/05 23:19
#import "PushView.h"
@interface PushView ()
@property (weak, nonatomic) UIImageView * imageView1;
@property (strong,nonatomic)UIDynamicAnimator * animator;
@property (strong,nonatomic)UIPushBehavior * push;
@property (assign,nonatomic)CGPoint beganPoint;
@property (assign,nonatomic)CGPoint endPoint;
@property (assign,nonatomic)CGPoint movePoint;
@end
@implementation PushView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [superinitWithFrame:frame]) {
self.backgroundColor = [UIColorwhiteColor];
[selfsetupViews];
}
return self;
}
- (void)setupViews{
UIImageView * imageView1 = [[UIImageViewalloc]init];
imageView1.backgroundColor = [UIColorredColor];
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 addSubview:imageView1];
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan:)];
[selfaddGestureRecognizer:pan];
/**
UIPushBehavior是继承自UIDynamicBehavior,UIDynamicBehavior是继承自NSObject
这个行为若有UICollisionBehavior如下,则这个行为最终停留在屏幕边缘,也就是他的父视图的边缘
如果没有UICollisionBehavior这个行为的载体最终会消失滑出父视图的边缘
*/
//UIPushBehaviorModeInstantaneous 力是瞬时的
_push = [[UIPushBehavioralloc]initWithItems:@[self.imageView1]mode:UIPushBehaviorModeInstantaneous];
//UIPushBehaviorModeContinuous 力是连续的
_push = [[UIPushBehavioralloc]initWithItems:@[self.imageView1]mode:UIPushBehaviorModeContinuous];
[self.animatoraddBehavior:_push];
//创建了之后默认是YES用这个属性来决定这个行为是否执行
NSLog(@"active %d ",_push.active);
//加一个UICollisionBehavior就不会出现在屏幕外面
UICollisionBehavior * collision = [[UICollisionBehavioralloc]initWithItems:@[self.imageView1]];
collision.translatesReferenceBoundsIntoBoundary =YES;
[self.animatoraddBehavior:collision];
}
- (void)pan:(UIPanGestureRecognizer *)pan{
CGPoint location = [pan locationInView:self];
if (UIGestureRecognizerStateBegan == pan.state) {
_beganPoint = location;
} elseif (UIGestureRecognizerStateChanged == pan.state){
_movePoint = location;
[selfsetNeedsDisplay];
} elseif (UIGestureRecognizerStateEnded == pan.state){
_endPoint = location;
_push.active =YES;
CGPoint offset =CGPointMake(_beganPoint.x -_endPoint.x,self.beganPoint.y -_endPoint.y);
_push.active =YES;
// 角度 默认的是0如果要配置力矢量UIPushBehavior,必须要设置magnitude和angle两个属性
_push.angle = -atan2f(offset.y, -offset.x);
// 推动行为的力矢量的大小默认是nil若为负则是力的反方向
_push.magnitude =sqrtf(offset.x * offset.x + offset.y * offset.y) /50;
//这个方法相当于设定angle和 magnitude
[_push setAngle:-atan2f(offset.y, -offset.x)magnitude:sqrtf(offset.x * offset.x + offset.y * offset.y) /50];
[selfclearLine];
}
}
- (void)drawRect:(CGRect)rect{
//绘制拉伸线路
CGContextRef context =UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, self.beganPoint.x,self.beganPoint.y);
CGContextAddLineToPoint(context,_movePoint.x,_movePoint.y);
CGContextSetLineWidth(context,5.0f);
CGFloat length[] = {15.0,5.0};
CGContextSetLineDash(context, 0.0, length, 2);
[[UIColorblueColor]setStroke];
CGContextDrawPath(context,kCGPathFillStroke);
}
- (void)clearLine{
_beganPoint = CGPointZero;
_movePoint =CGPointZero;
_endPoint =CGPointZero;
[selfsetNeedsDisplay];
}
- (UIDynamicAnimator *)animator{
if (_animator ==nil) {
_animator = [[UIDynamicAnimatoralloc]initWithReferenceView:self];
}
return_animator;
}
@end
- UIPushBehavior的简单使用
- UIPushBehavior-动画效果:推移行为
- UIPushBehavior-动画效果:推移行为
- iOS-UIDynamic物理仿真-推力-UIPushBehavior
- 简单易懂的使用
- ant的简单使用
- Log4j的简单使用
- TCPDUMP的简单使用
- IComparer的简单使用
- TreeView的简单使用
- CListCtrl的简单使用
- Rose的简单使用
- OpenSSL的简单使用
- CInternetSession的简单使用
- 标签的简单使用
- 触发器的简单使用
- nekohtml的简单使用
- FileUpload的简单使用
- 清除缓存
- 谓词 操作
- 详解js闭包
- Apple Pay开发详解
- ios 获取当前app的版本信息
- UIPushBehavior的简单使用
- 自己的代码记录
- 1015. Reversible Primes (20)
- oracle to_number
- 创建新的数据类型:类
- 设计模式kvc和kvo
- 《大话设计模式》——原则:单一职责原则
- 改进的KMP算法
- httpcliet、commons-httpclient、defaulthttpclient的区别和联系