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.
@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
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.