#import "ViewController.h"
@interface ViewController ()<UIDynamicAnimatorDelegate,UICollisionBehaviorDelegate>
{ UIDynamicAnimator *animator;
UIView
*view;
UIView
*view2;}
@end
@implementation ViewController
- (
void
)viewDidLoad { [
super
viewDidLoad];
// UIDynamicAnimator
/* UIDynamic是从ios7开始引入的一种新技术 属于UIKit框架 可以模拟现实生活中的物理现象 如碰撞 抖动 摆动 等 动力效果:如动吉他,电吉他有效果器 可以添加各种电子效果 动力效果也有一个效果器,叫做动力效果器,里面可以添加动力效果 电吉他可以叠加多个效果 动力效果也是一样 动力效果的使用步骤: 1、创建动力效果器(UIDynamicAnimator) 2、创建动力效果(Behavior)添加到对应的视图上 3、将动力效果添加到动力效果器中开始动力效果 再是用动力效果的时候必须遵守UIDynamicItem这个协议才可以使用动力效果 UIView默认遵守了UIDynamicItem协议 UIDynamic提供的动力效果 UIGravityBehavior:重力效果 UICollisionBehavior:碰撞效果 UIDynamicItemBehavior:动力元素效果 UIPushBehavior:推动效果 UISnapBehavior:迅速移动效果 UIAttachmentBehavior:附着效果 都继承自UIDynamicBehavior 动力效果器:UIDynamicAnimator 可以把UIDynamicAnimator看做动力效果的容器 它制定了动力效果的有效范围 在初始化的时候可以指定他的有效范围 - (instancetype)initWithReferenceView:(UIView*)view; 作用在哪一个view上 哪一个view就是他产生动力效果的有效范围 既然是容器 他还可以添加移除 动力效果 - (void)addBehavior:(UIDynamicBehavior *)behavior; 添加动力效果 - (void)removeBehavior:(UIDynamicBehavior *)behavior; 移除动力效果 - (void)removeAllBehaviors; 移除之前添加过的所有动力效果 动力效果器常用的属性 @property (nonatomic, readonly) UIView* referenceView;作用的区域 @property (nonatomic, readonly, copy) NSArray* behaviors;添加到效果器中的所有效果 @property (nonatomic, readonly, getter = isRunning) BOOL running;是否正在进行 @property (nonatomic, assign) id <UIDynamicAnimatorDelegate> delegate;可以检测开始暂停 - (void)dynamicAnimatorWillResume:(UIDynamicAnimator*)animator; - (void)dynamicAnimatorDidPause:(UIDynamicAnimator*)animator; */
animator = [[UIDynamicAnimator alloc]initWithReferenceView:
self
.view
];
// self.view是产生动力效果的区域
animator
.delegate
=
self
; view = [[
UIView
alloc]initWithFrame:CGRectMake(
0
,
0
,
100
,
100
)]; view
.layer
.cornerRadius
=
50
; view
.backgroundColor
= [
UIColor
colorWithRed:
0.345
green:
1.000
blue:
0.391
alpha:
1.000
]; [
self
.view
addSubview:view]; view2 = [[
UIView
alloc]initWithFrame:CGRectMake(
50
,
200
,
100
,
100
)]; view2
.layer
.cornerRadius
=
50
; view2
.backgroundColor
= [
UIColor
colorWithRed:
1.000
green:
0.282
blue:
0.298
alpha:
1.000
]; [
self
.view
addSubview:view2];
/* 碰撞效果 UICollisionBehavior 可以让物体之间实现碰撞效果 也可以通过添加边界(boundary)在边界实现碰撞效果 边界相关的方法 - (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier forPath:(UIBezierPath*)bezierPath; 添加一个贝塞尔曲线路径的边界 - (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2; 通过添加两个点连成的线 作为边界 - (UIBezierPath*)boundaryWithIdentifier:(id <NSCopying>)identifier; 通过ID找到边界路径 - (void)removeBoundaryWithIdentifier:(id <NSCopying>)identifier; 移除ID对应的边界 @property (nonatomic, readonly, copy) NSArray* boundaryIdentifiers; 边界数组 - (void)removeAllBoundaries;移除所有边界 碰撞的方式 typedef NS_OPTIONS(NSUInteger, UICollisionBehaviorMode) { UICollisionBehaviorModeItems = 1 << 0,元素碰撞 UICollisionBehaviorModeBoundaries = 1 << 1,边界碰撞 UICollisionBehaviorModeEverything = NSUIntegerMax 全体碰撞 } NS_ENUM_AVAILABLE_IOS(7_0); // 视图碰撞边界的时候 触发 - (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(id <NSCopying>)identifier atPoint:(CGPoint)p; - (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(id <NSCopying>)identifier; //两个元素相互碰撞 - (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p; - (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2; */
}
#pragma mark 动力效果器的代理方法
//启动
- (
void
)dynamicAnimatorWillResume:(UIDynamicAnimator*)animator{}
//暂停
- (
void
)dynamicAnimatorDidPause:(UIDynamicAnimator*)animator{}
#pragma mark 手指触摸屏幕添加动力效果
- (
void
)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject];
CGPoint
touchPoint = [touch locationInView:
self
.view
]; view
.center
= touchPoint;}- (
void
)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
#pragma mark 重力效果
// 把之前添加过的重力效果移除
[animator removeAllBehaviors];
// 初始化重力效果
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc]initWithItems:@[view]];
// 设置掉落方向
gravityBehavior
.gravityDirection
= CGVectorMake(
0
,
1
);
// 设置加速度 数值越大 碰撞效果越大
gravityBehavior
.magnitude
=
1
;
// 添加到效果器里面
[animator addBehavior:gravityBehavior];
#pragma mark 碰撞效果
/*// 初始化碰撞效果的对象 ,并给View添加碰撞的效果 UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc]initWithItems:@[view]];// 把动力效果器的范围当做边界 collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; collisionBehavior.collisionDelegate = self;#pragma mark 通过两个点画一条线当做边界// [collisionBehavior addBoundaryWithIdentifier:@"line" fromPoint:CGPointMake(0, 300) toPoint:CGPointMake(100, 600)];// [collisionBehavior addBoundaryWithIdentifier:@"line1" fromPoint:CGPointMake(375, 300) toPoint:CGPointMake(275, 600)];#pragma mark 通过一个圈来当做边界 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 200, 375, 375)]; [collisionBehavior addBoundaryWithIdentifier:@"圆形" forPath:path]; [animator addBehavior:collisionBehavior]; */
#pragma mark 两个视图之间的碰撞
UICollisionBehavior *coll = [[UICollisionBehavior alloc]initWithItems:@[view,view2]]; coll
.translatesReferenceBoundsIntoBoundary
=
YES
;
// 如果是两个元素之间相互碰撞 设置边界 也不起作用
coll
.collisionMode
= UICollisionBehaviorModeEverything; coll
.collisionDelegate
=
self
; [animator addBehavior:coll];}- (
void
)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(
id
<UIDynamicItem>)item1 withItem:(
id
<UIDynamicItem>)item2 atPoint:(
CGPoint
)p{}- (
void
)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(
id
<UIDynamicItem>)item1 withItem:(
id
<UIDynamicItem>)item2{}
#pragma mark 碰撞动力效果的代理方法
- (
void
)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(
id
<UIDynamicItem>)item withBoundaryIdentifier:(
id
<NSCopying>)identifier atPoint:(
CGPoint
)p{
NSLog
(@
"%f\n%f"
,p
.x
,p
.y
);}- (
void
)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(
id
<UIDynamicItem>)item withBoundaryIdentifier:(
id
<NSCopying>)identifier{}
重力碰撞
最新推荐文章于 2019-05-31 19:31:12 发布