Skip to content

Instantly share code, notes, and snippets.

@keicoder
Created February 13, 2014 04:42
Show Gist options
  • Select an option

  • Save keicoder/8969842 to your computer and use it in GitHub Desktop.

Select an option

Save keicoder/8969842 to your computer and use it in GitHub Desktop.
objective-c : make basic table view with custom background and jason data v.4
//make basic table view with custom background and jason data v.4
//Adding some menu dynamics and takes gesture velocity, add velocity to behavior
//DynamicSandwichViewController.m
@implementation DynamicSandwichViewController
{
NSMutableArray* _views; //use this variable to keep track of the added views
//Adding some menu dynamics
UIGravityBehavior* _gravity;
UIDynamicAnimator* _animator;
CGPoint _previousTouchPoint;
BOOL _draggingView;
}
#pragma mark - addRecipeAtOffset (add each recipe to the view)
- (UIView*)addRecipeAtOffset:(CGFloat)offset forSandwich:(NSDictionary*)sandwich {
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
...
//adding gesture and dynamics
// 1. add a gesture recognizer
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
[viewController.view addGestureRecognizer:pan];
// 2. create a collision
UICollisionBehavior* collision = [[UICollisionBehavior alloc]
initWithItems:@[view]];
[_animator addBehavior:collision];
// 3. lower boundary, where the view will come to rest, based on the bottom edge of the current view location
CGFloat boundary = view.frame.origin.y + view.frame.size.height+1;
NSLog(@"boundary %f : view.frame.origin.y %f + view.frame.size.height %f", boundary, view.frame.origin.y, view.frame.size.height);
CGPoint boundaryStart = CGPointMake(0.0, boundary);
NSLog(@"boundaryStart = x %f, y %f", boundaryStart.x, boundaryStart.y);
CGPoint boundaryEnd = CGPointMake(self.view.bounds.size.width,
boundary);
NSLog(@"boundaryEnd = x %f, y %f", self.view.bounds.size.width, boundary);
[collision addBoundaryWithIdentifier:@1
fromPoint:boundaryStart
toPoint:boundaryEnd];
// 4. apply some gravity
[_gravity addItem:view];
//adds dynamic item behavior for each of the recipe views
//It allows to change the physical properties of a dynamic item.
UIDynamicItemBehavior* itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[view]];
[_animator addBehavior:itemBehavior];
return view;
}
#pragma mark - finds UIDynamicItemBehavior
// iterates over the behaviors until it finds the one with the correct type (UIDynamicItemBehavior)
- (UIDynamicItemBehavior*) itemBehaviourForView:(UIView*)view {
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
for (UIDynamicItemBehavior* behaviour in _animator.behaviors) {
if (behaviour.class == [UIDynamicItemBehavior class]
&& [behaviour.items firstObject] == view) {
return behaviour;
}
}
return nil;
}
- (void)handlePan:(UIPanGestureRecognizer*)gesture {
CGPoint touchPoint = [gesture locationInView:self.view];
UIView* draggedView = gesture.view;
if (gesture.state == UIGestureRecognizerStateBegan) {
// 1. was the pan initiated from the top of the recipe?
CGPoint dragStartLocation = [gesture locationInView:draggedView];
if (dragStartLocation.y < 200.0f) {
_draggingView = YES;
_previousTouchPoint = touchPoint;
}
//If a drag is in progress, use the difference in Y locations between the previous and the current touches
//to offset the view’s center, making it move
} else if (gesture.state == UIGestureRecognizerStateChanged && _draggingView) {
// 2. handle dragging
CGFloat yOffset = _previousTouchPoint.y - touchPoint.y;
gesture.view.center = CGPointMake(draggedView.center.x,
draggedView.center.y - yOffset);
_previousTouchPoint = touchPoint;
} else if (gesture.state == UIGestureRecognizerStateEnded && _draggingView) {
[self addVelocityToView:draggedView fromGesture:gesture];
// 3. the gesture has ended
// updateItemUsingCurrentState: asks a dynamic animator to read the current state of a dynamic item,
// replacing the animator’s internal representation of the item’s state.
[_animator updateItemUsingCurrentState:draggedView];
_draggingView = NO; }
}
#pragma mark - addVelocityToView
//takes gesture velocity, removes X component, locates item behavior, adds velocity to behavior
- (void)addVelocityToView:(UIView*)view fromGesture:(UIPanGestureRecognizer*)gesture {
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
CGPoint vel = [gesture velocityInView:self.view];
vel.x = 0;
UIDynamicItemBehavior* behaviour = [self itemBehaviourForView:view];
[behaviour addLinearVelocity:vel forItem:view];
}
#pragma mark - 뷰 라이프 사이클
- (void)viewDidLoad
{
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));}
...
//Adding some menu dynamics
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_gravity = [[UIGravityBehavior alloc] init];
[_animator addBehavior:_gravity];
_gravity.magnitude = 4.0f; //making items fall more slowly than the default value of 1.0
...
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment