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

uikit_11

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

//
//  UIKitTestViewController.m
//  UIKitTest
//
//  Created by chenrs on 14-9-16.
//  Copyright (c) 2014年 chenrs. All rights reserved.
//

#import "UIKitTestViewController.h"

@interface UIKitTestViewController () <UICollisionBehaviorDelegate>
@property (strong, nonatomic) UIDynamicAnimator* animator;
@property (strong, nonatomic) UIGravityBehavior* gravity;
@property (strong, nonatomic) UICollisionBehavior* collision;
@property (nonatomic) BOOL firstContact;
@end

@implementation UIKitTestViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    __block UIKitTestViewController *blockSelf = self;
    __block int count=0;
    
    UIView* square = [[UIView alloc] initWithFrame:
                      CGRectMake(100, 0, 80, 80)];
    square.backgroundColor = [UIColor grayColor];
    [self.view addSubview:square];
    
    
    UIView* barrier = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 120, 20)];
    barrier.backgroundColor = [UIColor redColor];
    [self.view addSubview:barrier];
    
    
    _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    _gravity = [[UIGravityBehavior alloc] initWithItems:@[square]];
    [_animator addBehavior:_gravity];
    
    
    
    //_collision = [[UICollisionBehavior alloc] initWithItems:@[square, barrier]];
    _collision = [[UICollisionBehavior alloc] initWithItems:@[square]];
    _collision.translatesReferenceBoundsIntoBoundary = YES;
    
    
    _collision.action =  ^{
        /*NSLog(@"%@, %@",
              NSStringFromCGAffineTransform(square.transform),
              NSStringFromCGPoint(square.center));*/
        //if (!(count%2))
        {
            UIView* block = [[UIView alloc] initWithFrame:
                             CGRectMake(square.frame.origin.x, square.frame.origin.y, 80, 80)];
            
            //block.backgroundColor = [UIColor greenColor];
            
            block.layer.borderWidth = 1;
            block.layer.borderColor = [[UIColor grayColor] CGColor];
            
            [block setTransform:square.transform];
            
            [blockSelf.view addSubview:block];
        }
        //count++;
        
        
    };
    
    _collision.collisionDelegate = self;
    
    
    
    [_animator addBehavior:_collision];
    
    //添加了一个不可见的边界
    CGPoint rightEdge = CGPointMake(barrier.frame.origin.x +
                                    barrier.frame.size.width, barrier.frame.origin.y);
    
    [_collision addBoundaryWithIdentifier:@"barrier"
                                fromPoint:barrier.frame.origin
                                  toPoint:rightEdge];
    
    //添加弹性系数属性
    UIDynamicItemBehavior* itemBehaviour = [[UIDynamicItemBehavior alloc] initWithItems:@[square]];
    itemBehaviour.elasticity = 0.5;
    [_animator addBehavior:itemBehaviour];

}

- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id)item
   withBoundaryIdentifier:(id)identifier atPoint:(CGPoint)p
{
    //NSLog(@"Boundary contact occurred - %@", identifier);
    
    UIView* view = (UIView*)item;
    view.backgroundColor = [UIColor yellowColor];
    [UIView animateWithDuration:0.3 animations:
     ^{
        view.backgroundColor = [UIColor grayColor];
     }
     ];
    
    
    /*if (!_firstContact)
    {
        _firstContact = YES;
        
        UIView* square = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 80, 80)];
        square.backgroundColor = [UIColor grayColor];
        [self.view addSubview:square];
        
        [_collision addItem:square];
        [_gravity addItem:square];
        
        UIAttachmentBehavior* attach = [[UIAttachmentBehavior alloc] initWithItem:view
                                                                   attachedToItem:square];
        [_animator addBehavior:attach];
    }*/
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Comments are closed.