zoukankan      html  css  js  c++  java
  • UIDynamic(重力行为+碰撞检测)

    一、重力行为

    说明:给定重力方向、加速度,让物体朝着重力方向掉落

    1.方法

    (1)UIGravityBehavior的初始化

      - (instancetype)initWithItems:(NSArray *)items;

        item参数 :里面存放着物理仿真元素

    (2)UIGravityBehavior常见方法

      - (void)addItem:(id <UIDynamicItem>)item;

        添加1个物理仿真元素

      - (void)removeItem:(id <UIDynamicItem>)item;

        移除1个物理仿真元素

    2.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²)

    二、碰撞行为

    1.简介

    说明:可以让物体之间实现碰撞效果

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

    2.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;

    3.UICollisionBehavior常见用法

    @property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;

      是否以参照视图的bounds为边界

    - (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(UIEdgeInsets)insets;

      设置参照视图的bounds为边界,并且设置内边距

    @property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;

      碰撞模式(分为3种,元素碰撞、边界碰撞、全体碰撞)

    @property (nonatomic, assign, readwrite) id <UICollisionBehaviorDelegate> collisionDelegate;

      代理对象(可以监听元素的碰撞过程)

    代码:

      1 //
      2 //  YYViewController.m
      3 //  12-重力行为和碰撞行为
      4 //
      5 //  Created by apple on 14-8-6.
      6 //  Copyright (c) 2014年 yangyong. All rights reserved.
      7 //
      8 
      9 #import "YYViewController.h"
     10 
     11 @interface YYViewController ()
     12 @property (weak, nonatomic) IBOutlet UIView *redView;
     13 
     14 @property (weak, nonatomic) IBOutlet UIProgressView *block1;
     15 @property (weak, nonatomic) IBOutlet UISegmentedControl *block2;
     16 
     17 @property(nonatomic,strong)UIDynamicAnimator *animator;
     18 @end
     19 
     20 @implementation YYViewController
     21 -(UIDynamicAnimator *)animator
     22 {
     23     if (_animator==nil) {
     24         //创建物理仿真器(ReferenceView:参照视图,设置仿真范围)
     25         self.animator=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
     26     }
     27     return _animator;
     28 }
     29 - (void)viewDidLoad
     30 {
     31     [super viewDidLoad];
     32     
     33     
     34     //设置红色view的角度
     35     self.redView.transform=CGAffineTransformMakeRotation(M_PI_4);
     36 }
     37 
     38 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
     39 {
     40     //1.重力行为
     41 //    [self testGravity];
     42     //2.重力行为+碰撞检测
     43 //        [self testGravityAndCollsion];
     44     //3.测试重力的一些属性
     45 //    [self testGravityAndCollsion2];
     46     //用2根线作为边界
     47 //    [self testGravityAndCollision3];
     48     //4.用圆作为边界
     49     [self testGravityAndCollision4];
     50 }
     51 
     52 /**
     53  *  重力行为
     54  */
     55 -(void)testGravity
     56 {
     57     //1.创建仿真行为(进行怎样的仿真效果?)
     58     //重力行为
     59     UIGravityBehavior *gravity=[[UIGravityBehavior alloc] init];
     60     //2.添加物理仿真元素
     61     [gravity addItem:self.redView];
     62     //3.执行仿真,让物理仿真元素执行仿真行为
     63     [self.animator addBehavior:gravity];
     64 }
     65 /**
     66  *  重力行为+碰撞检测
     67  */
     68 -(void)testGravityAndCollsion
     69 {
     70     //1.重力行为
     71     UIGravityBehavior *gravity=[[UIGravityBehavior alloc]init];
     72     [gravity addItem:self.redView];
     73     
     74     //2碰撞检测行为
     75     UICollisionBehavior *collision=[[UICollisionBehavior alloc]init];
     76     [collision addItem:self.redView];
     77     [collision addItem:self.block1];
     78     [collision addItem:self.block2];
     79     
     80     //让参照视图的边框成为碰撞检测的边界
     81     collision.translatesReferenceBoundsIntoBoundary=YES;
     82     
     83     //3.执行仿真
     84     [self.animator addBehavior:gravity];
     85     [self.animator addBehavior:collision];
     86 }
     87 
     88 /**
     89  *  测试重力行为的属性
     90  */
     91 -(void)testGravityAndCollsion2
     92 {
     93     //1.重力行为
     94     UIGravityBehavior *gravity=[[UIGravityBehavior alloc]init];
     95     //(1)设置重力的方向(是一个角度)
     96     //    gravity.angle=(M_PI_2-M_PI_4);
     97     //(2)设置重力的加速度,重力的加速度越大,碰撞就越厉害
     98     gravity.magnitude=1000;
     99     //(3)设置重力的方向(是一个二维向量)
    100     gravity.gravityDirection=CGVectorMake(0, 1);
    101     [gravity addItem:self.redView];
    102     
    103     //2碰撞检测行为
    104     UICollisionBehavior *collision=[[UICollisionBehavior alloc]init];
    105     [collision addItem:self.redView];
    106     [collision addItem:self.block1];
    107     [collision addItem:self.block2];
    108     
    109     //让参照视图的边框成为碰撞检测的边界
    110     collision.translatesReferenceBoundsIntoBoundary=YES;
    111     
    112     //3.执行仿真
    113     [self.animator addBehavior:gravity];
    114     [self.animator addBehavior:collision];
    115     
    116 }
    117 
    118 /**
    119  *  用圆作为边界
    120  */
    121 - (void)testGravityAndCollision4
    122 {
    123     // 1.重力行为
    124     UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init];
    125     [gravity addItem:self.redView];
    126 //    [gravity addItem:self.block1];
    127 //    [gravity addItem:self.block2];
    128     
    129     // 2.碰撞检测行为
    130     UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
    131     [collision addItem:self.redView];
    132     [collision addItem:self.block1];
    133     [collision addItem:self.block2];
    134     
    135     // 添加一个椭圆为碰撞边界
    136     UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 320, 320)];
    137     [collision addBoundaryWithIdentifier:@"circle" forPath:path];
    138     
    139     // 3.开始仿真
    140     [self.animator addBehavior:gravity];
    141     [self.animator addBehavior:collision];
    142 }
    143 
    144 /**
    145  *  用2根线作为边界
    146  */
    147 - (void)testGravityAndCollision3
    148 {
    149     // 1.重力行为
    150     UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init];
    151     [gravity addItem:self.redView];
    152     
    153     // 2.碰撞检测行为
    154     UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
    155     [collision addItem:self.redView];
    156     [collision addItem:self.block1];
    157     [collision addItem:self.block2];
    158     CGPoint startP = CGPointMake(0, 160);
    159     CGPoint endP = CGPointMake(320, 568);
    160     [collision addBoundaryWithIdentifier:@"line1" fromPoint:startP toPoint:endP];
    161     CGPoint startP1 = CGPointMake(320, 0);
    162     [collision addBoundaryWithIdentifier:@"line2" fromPoint:startP1 toPoint:endP];
    163     //    collision.translatesReferenceBoundsIntoBoundary = YES;
    164     
    165     // 3.开始仿真
    166     [self.animator addBehavior:gravity];
    167     [self.animator addBehavior:collision];
    168 }
    169 @end
  • 相关阅读:
    python下RSA 加密/解密,签名/验证
    python字符串str和字节数组相互转化
    代码存档
    windows 7 安装 scrapy
    scrapy 爬取自己的博客
    win32api 找不到指定的模块
    SQLite3日期与时间,常见函数
    sqlite3日期数据类型
    myeclipse集成maven
    UIKit class hierarchy
  • 原文地址:https://www.cnblogs.com/bigshow1949/p/5806203.html
Copyright © 2011-2022 走看看