是否可以将 UIView 固定但作为碰撞对象?在我的示例中,一个盒子视图掉落并与楼层视图发生碰撞。我希望地板视图保持在它的位置,但它在被盒子击中后正在移动。如何将它粘在一个地方(我不想放大对象或使用 UIView 以外的其他对象(UIBezierPath 等))。
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *start = [[UIButton alloc]initWithFrame:CGRectMake(200, 20, 40, 40)];
start.backgroundColor = [UIColor redColor];
[start addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
[main addSubview:start];
self.box1 = [[UIView alloc]initWithFrame:CGRectMake(50, 0, 20, 20)];
self.box1.backgroundColor = [UIColor redColor];
[main addSubview:self.box1];
self.floor = [[UIView alloc]initWithFrame:CGRectMake(0, main.frame.size.height-20, main.frame.size.width, 200)];
self.floor.backgroundColor = [UIColor lightGrayColor];
[main addSubview:self.floor];
}
- (void) start {
self.animation = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[self.box1]];
[self.animation addBehavior:gravity];
UICollisionBehavior *collision = [[UICollisionBehavior alloc]initWithItems:@[self.box1, self.floor]];
[self.animation addBehavior:collision];
}