简体   繁体   English

修复了UIView发生碰撞的问题

[英]Fixed UIView for collision

Is it possible to place an UIView fixed but as collision-object? 是否可以将UIView固定但作为碰撞对象放置? In my example is a box-view falling down and collide with the floor-view. 在我的示例中,一个盒子视图掉下来并与地板视图发生碰撞。 I want the floor-view to stay at it's place but it's moving after it's been hit by the box. 我希望将地板视图留在原处,但在被盒子击中后它仍在移动。 How can I glue it in one place (I don't want to enlarge the object or use other objects than UIView (UIBezierPath etc)). 如何将其粘贴在一个位置(我不想扩大对象或使用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];
}

One solution to your problem (which is a quite simple setup) could be to create an UIView that will serve as a reference view. 解决您的问题的一种方法(这是一个非常简单的设置),可以创建一个UIView用作参考视图。 Set it's frame so the bottom of the view will be your floor. 设置框架,以使视图的底部成为您的地板。 Then add your box as a subview for that reference view, add collision behaviour with translatesReferenceBoundsIntoBoundary set to YES and you have a box falling on the floor. 然后将您的框添加为该参考视图的子视图,添加碰撞行为并将translatesReferenceBoundsIntoBoundary设置为YES,并且您有一个框掉在地上。

// Reference view
CGRect referenceFrame = CGRectMake(0, 0, 320, 500); // the floor will be at y = 500
UIView *referenceView = [[UIView alloc] initWithFrame:referenceFrame];
[self.view addSubview:referenceView];

// Box
CGRect boxFrame = CGRectMake(50, 50, 10, 10);
UIView *boxView = [[UIView alloc] initWithFrame:boxFrame];
boxView.backgroundColor = [UIColor redColor];

[referenceView addSubview:boxView];

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:referenceView];

// Gravity
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[boxView]];
[self.animator addBehavior:gravity];

UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[boxView]];
collision.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:collision];

You can provide a pretty visual for the floor outside of referenceView 您可以为referenceView外部的地板提供漂亮的视觉效果

I used UIDynamicItemBehavior for my view and set the following property to be true. 我将UIDynamicItemBehavior用于我的视图,并将以下属性设置为true。

@property(nonatomic, getter=isAnchored) BOOL anchored;

This locked the UIView in place. 这将UIView锁定到位。 Do what you were doing above to add the item to the behavior and add the behavior to the animator. 进行上述操作,以将项目添加到行为并将行为添加到动画师。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM