UIKit 自由下落图形轨迹的显示

uikit_11

在UIKit中有一个下落轨迹的图形,我将实验出的实现代码放置在下面:

  1//
  2//  UIKitTestViewController.m
  3//  UIKitTest
  4//
  5//  Created by chenrs on 14-9-16.
  6//  Copyright (c) 2014年 chenrs. All rights reserved.
  7//
  8
  9#import "UIKitTestViewController.h"
 10
 11@interface UIKitTestViewController () <UICollisionBehaviorDelegate>
 12@property (strong, nonatomic) UIDynamicAnimator* animator;
 13@property (strong, nonatomic) UIGravityBehavior* gravity;
 14@property (strong, nonatomic) UICollisionBehavior* collision;
 15@property (nonatomic) BOOL firstContact;
 16@end
 17
 18@implementation UIKitTestViewController
 19
 20- (void)viewDidLoad
 21{
 22    [super viewDidLoad];
 23	// Do any additional setup after loading the view, typically from a nib.
 24    
 25    __block UIKitTestViewController *blockSelf = self;
 26    __block int count=0;
 27    
 28    UIView* square = [[UIView alloc] initWithFrame:
 29                      CGRectMake(100, 0, 80, 80)];
 30    square.backgroundColor = [UIColor grayColor];
 31    [self.view addSubview:square];
 32    
 33    
 34    UIView* barrier = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 120, 20)];
 35    barrier.backgroundColor = [UIColor redColor];
 36    [self.view addSubview:barrier];
 37    
 38    
 39    _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
 40    _gravity = [[UIGravityBehavior alloc] initWithItems:@[square]];
 41    [_animator addBehavior:_gravity];
 42    
 43    
 44    
 45    //_collision = [[UICollisionBehavior alloc] initWithItems:@[square, barrier]];
 46    _collision = [[UICollisionBehavior alloc] initWithItems:@[square]];
 47    _collision.translatesReferenceBoundsIntoBoundary = YES;
 48    
 49    
 50    _collision.action =  ^{
 51        /*NSLog(@"%@, %@",
 52              NSStringFromCGAffineTransform(square.transform),
 53              NSStringFromCGPoint(square.center));*/
 54        //if (!(count%2))
 55        {
 56            UIView* block = [[UIView alloc] initWithFrame:
 57                             CGRectMake(square.frame.origin.x, square.frame.origin.y, 80, 80)];
 58            
 59            //block.backgroundColor = [UIColor greenColor];
 60            
 61            block.layer.borderWidth = 1;
 62            block.layer.borderColor = [[UIColor grayColor] CGColor];
 63            
 64            [block setTransform:square.transform];
 65            
 66            [blockSelf.view addSubview:block];
 67        }
 68        //count++;
 69        
 70        
 71    };
 72    
 73    _collision.collisionDelegate = self;
 74    
 75    
 76    
 77    [_animator addBehavior:_collision];
 78    
 79    //添加了一个不可见的边界
 80    CGPoint rightEdge = CGPointMake(barrier.frame.origin.x +
 81                                    barrier.frame.size.width, barrier.frame.origin.y);
 82    
 83    [_collision addBoundaryWithIdentifier:@"barrier"
 84                                fromPoint:barrier.frame.origin
 85                                  toPoint:rightEdge];
 86    
 87    //添加弹性系数属性
 88    UIDynamicItemBehavior* itemBehaviour = [[UIDynamicItemBehavior alloc] initWithItems:@[square]];
 89    itemBehaviour.elasticity = 0.5;
 90    [_animator addBehavior:itemBehaviour];
 91
 92}
 93
 94- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id)item
 95   withBoundaryIdentifier:(id)identifier atPoint:(CGPoint)p
 96{
 97    //NSLog(@"Boundary contact occurred - %@", identifier);
 98    
 99    UIView* view = (UIView*)item;
100    view.backgroundColor = [UIColor yellowColor];
101    [UIView animateWithDuration:0.3 animations:
102     ^{
103        view.backgroundColor = [UIColor grayColor];
104     }
105     ];
106    
107    
108    /*if (!_firstContact)
109    {
110        _firstContact = YES;
111        
112        UIView* square = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 80, 80)];
113        square.backgroundColor = [UIColor grayColor];
114        [self.view addSubview:square];
115        
116        [_collision addItem:square];
117        [_gravity addItem:square];
118        
119        UIAttachmentBehavior* attach = [[UIAttachmentBehavior alloc] initWithItem:view
120                                                                   attachedToItem:square];
121        [_animator addBehavior:attach];
122    }*/
123}
124
125- (void)didReceiveMemoryWarning
126{
127    [super didReceiveMemoryWarning];
128    // Dispose of any resources that can be recreated.
129}
130
131@end