我是ios编程的初学者。当我使用 UiCollisionBehavior 设置放置项目的边界时,我将以下代码添加到 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
但是,myView 并没有下降,只是停留在原来的位置。有人可以告诉我原因吗?