1103-UIDynamic-只有50分钟

具体代码工作中用到在开始

//
//  HMViewController.m
//  01-UIDynamic

#import "HMViewController.h"

@interface HMViewController ()
/**
 *  红色view
 */
@property (weak, nonatomic) IBOutlet UIView *redView;
/**
 *  物理仿真器
 */
@property (nonatomic, strong) UIDynamicAnimator *anim;

@property (weak, nonatomic) IBOutlet UISegmentedControl *st;
@end

@implementation HMViewController

- (UIDynamicAnimator *)anim
{
    if (!_anim) {
        _anim = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    }
    return _anim;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.获取当前触摸的手指
    UITouch *touch = [touches anyObject];
    // 2.更具手指取出位置
    CGPoint point = [touch locationInView:touch.view];
    
    // 吸附行为
    // 1.创建物理仿真器
    // 2.创建物理仿真行为
    UISnapBehavior *snapB = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:point];
    
    // 设置吸附行为的"减震"
    snapB.damping = 0;
    
    // 注意: 吸附行为默认只能吸附一次, 如果多次吸附必须从仿真器中移除再重新添加
    [self.anim removeAllBehaviors];
    
    // 3.将物理仿真行为添加到仿真器中
    [self.anim addBehavior:snapB];
    
}

- (void)test2
{
    // 碰撞
    // 1.创建物理仿真器
    // 2.创建物理仿真行为
    UIGravityBehavior *gravigtyB = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
    //    gravigtyB.magnitude = 100;
    
    // 创建碰撞仿真行为
    UICollisionBehavior *collisionB = [[UICollisionBehavior alloc] initWithItems:@[self.redView, self.st]];
    // 设置碰撞的边界
    //    collisionB.translatesReferenceBoundsIntoBoundary = YES;
    
    // 添加直线边界
    //    [collisionB addBoundaryWithIdentifier:@"line" fromPoint:CGPointMake(0, 200) toPoint:CGPointMake(320, 420)];
    
    // 添加图形的边界
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.view.frame];
    [collisionB addBoundaryWithIdentifier:@"abc" forPath:path];
    
    
    // 3.将物理仿真行为添加到仿真器中
    [self.anim addBehavior:gravigtyB];
    [self.anim addBehavior:collisionB];
}

/**
 *  重力
 */
- (void)test
{
    // 演示重力行为
    // 1.创建物理仿真器
    // 并且指定了当前控制器的view作为仿真范围
    //    UIDynamicAnimator *anim = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    
    // 2.创建物理仿真行为
    // 并且指定红色为作为仿真元素
    UIGravityBehavior *gravityB = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
    
    
    // 设置重力的方向
    //    gravityB.gravityDirection = CGVectorMake(1, 0);
    //    gravityB.gravityDirection = CGVectorMake(0, -1);
    //    gravityB.gravityDirection = CGVectorMake(1, 1);
    // 设置重力的角度
    //    gravityB.angle = M_PI_2;
    
    // 设置重力的加速度
    gravityB.magnitude = 100.0;
    
    // 3.将物理仿真行为添加到仿真器中
    [self.anim addBehavior:gravityB];
}
@end
View Code

其他代码在百度网盘里

 

UIGravityBehavior常见属性
@property (nonatomic, readonly, copy) NSArray* items;
添加到重力行为中的所有物理仿真元素

@property (readwrite, nonatomic) CGVector gravityDirection;
重力方向(是一个二维向量)

@property (readwrite, nonatomic) CGFloat angle;
重力方向(是一个角度,以x轴正方向为0°,顺时针正数,逆时针负数)

@property (readwrite, nonatomic) CGFloat magnitude;
量级(用来控制加速度,1.0代表加速度是1000 points /second²)

碰撞行为

可以让物体之间实现碰撞效果
可以通过添加边界(boundary),让物理碰撞局限在某个空间中

UICollisionBehavior边界相关的方法
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier forPath:(UIBezierPath*)bezierPath;
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2;
- (UIBezierPath*)boundaryWithIdentifier:(id <NSCopying>)identifier;
- (void)removeBoundaryWithIdentifier:(id <NSCopying>)identifier;
@property (nonatomic, readonly, copy) NSArray* boundaryIdentifiers;
- (void)removeAllBoundaries;

UICollisionBehavior常见用法
p@property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;
ü是否以参照视图的bounds为边界
ü
p- (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(UIEdgeInsets)insets;
ü设置参照视图的bounds为边界,并且设置内边距
ü
p@property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;
ü碰撞模式(分为3种,元素碰撞、边界碰撞、全体碰撞)
p
p@property (nonatomic, assign, readwrite) id <UICollisionBehaviorDelegate> collisionDelegate;
ü代理对象(可以监听元素的碰撞过程)


捕捉行为

可以让物体迅速冲到某个位置(捕捉位置),捕捉到位置之后会带有一定的震动

UISnapBehavior的初始化
- (instancetype)initWithItem:(id <UIDynamicItem>)item snapToPoint:(CGPoint)point;

UISnapBehavior常见属性
@property (nonatomic, assign) CGFloat damping;
用于减幅、减震(取值范围是0.0 ~ 1.0,值越大,震动幅度越小)

UISnapBehavior使用注意
如果要进行连续的捕捉行为,需要先把前面的捕捉行为从物理仿真器中移除

 

 

 

-----------

 

posted @ 2016-03-22 22:35  海龙王来了  阅读(147)  评论(0编辑  收藏  举报
友情链接:废钢破碎机  带式压滤机