UIKit Dynamics 碰撞 - 保持 Barriers 元素静止

Posted

技术标签:

【中文标题】UIKit Dynamics 碰撞 - 保持 Barriers 元素静止【英文标题】:UIKit Dynamics collision - keeping the Barriers elements stationary 【发布时间】:2014-04-01 15:26:40 【问题描述】:

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

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

@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

【问题讨论】:

现在没时间写一个完整的答案,但是看看 UIAttachmentBehavior。 感谢撤消,它正在部分工作,现在红色的家伙被锁定但仍然围绕中心点旋转......(不过效果很酷;)) 你能发布你到目前为止的代码吗?如果你展示它,我想我可以弄清楚如何防止它们旋转。 刚刚将它添加到“addTheBarrier”函数 - UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:barrier attachToAnchor:barrier.center]; [_animator addBehavior:attachment]; 【参考方案1】:

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

//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];

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

【讨论】:

以上是关于UIKit Dynamics 碰撞 - 保持 Barriers 元素静止的主要内容,如果未能解决你的问题,请参考以下文章

UIKit Dynamics UICollisionBehavior 无反弹的碰撞

iOS 力学动画生成器UIKit Dynamics 之碰撞效果解说

CollectionView + UIKit Dynamics 在 performBatchUpdates 上崩溃:

UIKit Dynamics 和 UIKit Animation 有啥区别?

UIKit Dynamics 的性能问题

UIKit Dynamics:识别圆形和边界