Reputation: 11
i am a beginner in ios programming. When i am using the UiCollisionBehavior to set the boundaries for the dropping items, i added the following code to the ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
myView.backgroundColor = [UIColor grayColor];
UIView *barrier = [[UIView alloc]initWithFrame:CGRectMake(0, 300, 130, 20)];
barrier.backgroundColor = [UIColor redColor];
[self.view addSubview: barrier];
[self.view addSubview:myView];
_animator = [[UIDynamicAnimator alloc]initWithReferenceView:myView];
_gravity = [[UIGravityBehavior alloc]initWithItems:@[myView]];
[_animator addBehavior:_gravity];
_collision = [[UICollisionBehavior alloc]
initWithItems:@[myView]];
_collision.translatesReferenceBoundsIntoBoundary= YES;
[_animator addBehavior:_collision];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
however, the myView did not drop and just stayed at the original position. Can someone tell me the reason?
Upvotes: 0
Views: 396
Reputation: 160
Replace this line, you are not adding barrier in UICollisionBehavior
_collision = [[UICollisionBehavior alloc] initWithItems:@[myView, barrier]];
Upvotes: 0