Created
February 9, 2014 09:27
-
-
Save keicoder/8896630 to your computer and use it in GitHub Desktop.
objective-c : UIKit Dynamics 기초 (view, barrier, gravity, collision, elasticity, attachmentBehavior)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//UIKit Dynamics 기초 (view, barrier, gravity, collision, elasticity, attachmentBehavior) | |
//ViewController.m | |
//.m | |
#import "ViewController.h" | |
//UICollisionBehaviorDelegate : receive notifications when items collide | |
@interface ViewController () <UICollisionBehaviorDelegate> { | |
//ivars | |
UIDynamicAnimator* _animator; | |
UIGravityBehavior* _gravity; | |
UICollisionBehavior* _collision; | |
BOOL _firstContact; | |
} | |
@end | |
@implementation ViewController | |
#define debug 1 | |
- (void)viewDidLoad | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
[super viewDidLoad]; | |
//square view | |
UIView* square = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; //x,y,width,height | |
square.backgroundColor = [UIColor grayColor]; | |
[self.view addSubview:square]; | |
//add an immovable barrier view | |
UIView* barrier = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 130, 20)]; | |
barrier.backgroundColor = [UIColor redColor]; | |
[self.view addSubview:barrier]; | |
//add gravity | |
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; //UIKit physics engine | |
_gravity = [[UIGravityBehavior alloc] initWithItems:@[square]]; | |
[_animator addBehavior:_gravity]; | |
//add Invisible boundaries and collision behavior | |
_collision = [[UICollisionBehavior alloc] initWithItems:@[square]]; | |
_collision.collisionDelegate = self; //receive notifications when items collide | |
// add a boundary that coincides with the top edge | |
CGPoint rightEdge = CGPointMake(barrier.frame.origin.x + | |
barrier.frame.size.width, | |
barrier.frame.origin.y); | |
[_collision addBoundaryWithIdentifier:@"barrier" fromPoint:barrier.frame.origin toPoint:rightEdge]; | |
_collision.translatesReferenceBoundsIntoBoundary = YES; //use the bounds of the reference view | |
[_animator addBehavior:_collision]; | |
//block code (dynamic behavior's action property) | |
//스퀘어 뷰의 경로 로그로 찍어보기 | |
_collision.action = ^{ | |
// NSLog(@"behavior's action property : %@, %@", | |
// NSStringFromCGAffineTransform(square.transform), | |
// NSStringFromCGPoint(square.center)); | |
}; | |
//creates an item behavior, associates it with the square | |
UIDynamicItemBehavior* itemBehaviour = [[UIDynamicItemBehavior alloc] initWithItems:@[square]]; | |
itemBehaviour.elasticity = 0.6; | |
[_animator addBehavior:itemBehaviour]; | |
} | |
#pragma mark - collision behavior delegate methods | |
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
NSLog(@"Boundary contact occurred - %@", identifier); | |
//changes the background color of the colliding item to yellow, and then fades it back to gray again | |
UIView *view = (UIView *)item; | |
view.backgroundColor = [UIColor yellowColor]; | |
[UIView animateWithDuration:0.3 animations:^{ | |
view.backgroundColor = [UIColor grayColor]; | |
}]; | |
//attachment behavior between the barrier and the square | |
if (!_firstContact) { | |
_firstContact = YES; | |
UIView* square = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 100, 100)]; | |
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]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment