2

I am trying to create some animation in an iOS app I am developing. I have a box that falls until it collides with a bar. I also added a bounce to the box for the impact on the bar. What I am trying to add now is a behavior on the bar so when the box hits the bar the reaction is a slight spring. I tried adding a UIAttachmentBehavior but couldn't figure out how to implement it correctly. I have seen the WWDC videos and other but I can not get it to work in this setting. If you could show me in this example how to implement it that would be great.

enter image description here

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _mainView = [[UIView alloc] initWithFrame:[[self view] bounds]];
    [_mainView setBackgroundColor:[UIColor clearColor]];
    [[self view] addSubview: _mainView];

    _objectView = 
     [[UIView alloc] initWithFrame:CGRectMake(_mainView.bounds.size.width/2-40, 40,
                                              80, 80)];
    [_objectView setBackgroundColor:[UIColor redColor]];
    [_mainView addSubview: _objectView];

    _barView = 
     [[UIView alloc] initWithFrame:CGRectMake(_mainView.bounds.size.width/2-50, 
                                              (_mainView.bounds.size.height/5) * 4, 
                                              100, 3)];
    [_barView setBackgroundColor:[UIColor blackColor]];
    [_mainView addSubview: _barView];

    //----------------------------------

    _animator = [[UIDynamicAnimator alloc] initWithReferenceView:_mainView];

    BounceCustomBehavior *bouncyBehavior = 
     [[BounceCustomBehavior alloc] 
                     initWithItems:@[_objectView] 
                            objects:[NSArray arrayWithObjects:_barView, nil]];
    [_animator addBehavior:bouncyBehavior];

    //-----------------------------------


}

#import "BounceCustomBehavior.h"

@implementation BounceCustomBehavior
-(instancetype)initWithItems:(NSArray *)items objects:(NSArray *)collisionObjs {
    if (!(self = [super init])) return nil;

    UIGravityBehavior* gravityBehavior = [[UIGravityBehavior alloc] initWithItems:items];
    [self addChildBehavior:gravityBehavior];

    UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:items];
    collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;

    for (UIView * view in collisionObjs) {
        CGPoint rightEdge = CGPointMake(view.frame.origin.x +
                                        view.frame.size.width, view.frame.origin.y);
        [collisionBehavior addBoundaryWithIdentifier:@""
                                    fromPoint:view.frame.origin
                                      toPoint:rightEdge];
    }
    [self addChildBehavior:collisionBehavior];

    UIDynamicItemBehavior *elasticityBehavior = [[UIDynamicItemBehavior alloc] initWithItems:items];
    elasticityBehavior.elasticity = 0.3f;
    [self addChildBehavior:elasticityBehavior];

    return self;
}
@end
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
BDGapps
  • 3,318
  • 10
  • 56
  • 75

1 Answers1

1

While attachment behaviors are very powerful, I first suggest you try attaching the line to a snap behavior and see if it works for you. Typically you need several attachment behaviors (often 4) in order to stabilize an item. Snap gives similar effects with a single, often easier to use, behavior.

As a separate issue, you're setting up your collisions incorrectly. Just add all of your views to a collision behavior (using addItem:). You don't need to create a bunch of boundaries.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • my problem with snap is that the bar tilts and block slides off. It doesn't dip down and pop back up with the block on it. – BDGapps Oct 31 '13 at 18:58
  • Attach the bar to a UIDynamicItemBehavior with allowsRotation=NO – Rob Napier Oct 31 '13 at 19:55
  • how to i make it so it snaps back to the same spot even no matter the weight – BDGapps Oct 31 '13 at 22:57
  • Add a delegate to the collision. When the collision occurs, remove the colliding item from the gravity behavior, and add it to an attachment behavior with the beam (so it doesn't bounce off). You have to think in terms of the physics. If you don't want the platform to sag, then you have to remove the load. – Rob Napier Nov 01 '13 at 14:10