gpt4 book ai didi

ios - UIKit Dynamics collision - 保持 Barriers 元素静止

转载 作者:搜寻专家 更新时间:2023-10-30 20:17:49 25 4
gpt4 key购买 nike

如何防止作为红色元素的“障碍”移动 - 同时允许“下落”元素(灰色/UIView* 方形)下落并与障碍互动?

有很多灰色元素,很多红色障碍...

enter image description here

@interface MainViewController ()
{
UIDynamicAnimator* _animator;
UIGravityBehavior* _gravity;
UICollisionBehavior* _collision;
//
NSMutableArray *collisionArray;
}

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//
}
return self;
}



- (void)viewDidLoad
{
[super viewDidLoad];
//

collisionArray = [[NSMutableArray alloc] init];
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
[self addTheBarrier];


}


-(void)addTheBarrier
{
for (int i=0; i<20; i++) {
UIView* barrier = [[UIView alloc] initWithFrame:CGRectMake(i*16, 250, 10, 10)];
barrier.backgroundColor = [UIColor redColor];
[self.view addSubview:barrier];


CGPoint rightEdge = CGPointMake(barrier.frame.origin.x +
barrier.frame.size.width, barrier.frame.origin.y);
[_collision addBoundaryWithIdentifier:@"barrier"
fromPoint:barrier.frame.origin
toPoint:rightEdge];
[collisionArray addObject:barrier];
}
[self startDynamic];
}



#pragma mark - Dynamics

-(void)startDynamic
{
UIView* square = [[UIView alloc] initWithFrame:CGRectMake((arc4random()%250)+10, 100, 10, 10)];
square.backgroundColor = [UIColor grayColor];

[self.view addSubview:square];
[collisionArray addObject:square];

_collision = [[UICollisionBehavior alloc] initWithItems:collisionArray];


_gravity = [[UIGravityBehavior alloc] initWithItems:@[square]];
[_animator addBehavior:_gravity];
//
UIDynamicItemBehavior* itemBehaviour = [[UIDynamicItemBehavior alloc] initWithItems:@[square]];
itemBehaviour.elasticity = 0.85;
[_animator addBehavior:itemBehaviour];

//
UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[ square ] mode:UIPushBehaviorModeContinuous];
[push setPushDirection:CGVectorMake(0, 0)];

[_animator addBehavior:push];
[_collision setTranslatesReferenceBoundsIntoBoundary:NO];
[_animator addBehavior:_collision];

[self performSelector:@selector(startDynamic) withObject:nil afterDelay:0.3 ];

}


@end

最佳答案

您需要将此添加到您的动态代码中:

//Attachment behavior that you figured out:

UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:barrier attachedToAnchor:barrier.center];
[_animator addBehavior:attachment];

// To keep the views from spinning:

UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[barrier]];
itemBehavior.allowsRotation = NO;
[_animator addBehavior:itemBehavior];

我们正在制作附件行为以防止 View 在整个屏幕上弹跳,然后我们使用 UIDynamicItemBehavior微调这些项目的行为 - 特别是防止它们旋转。

关于ios - UIKit Dynamics collision - 保持 Barriers 元素静止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22790233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com