Detecting and reacting to collisions Between UI Components

Problem

You want to specify collision boundaries between your UI components on the screen so that they will not overlap one another.

Solution

Instantiate an object of type UICollisionBehavior and attach it to your animator ob‐ ject. Set the translatesReferenceBoundsIntoBoundary property of your collision be‐havior to YES and ensure that your animator is initialized with your superview as its reference value. This will ensure that the subviews that are the targets of your collision behavior (as will be discussed soon) will not go outside the boundaries of your super‐ view.

Discussion

A collision behavior of type UICollisionBehavior takes in objects that conform to the UIDynamicItem protocol. All views of type UIView already conform to this protocol, so all you have to do is instantiate your views and add them to the collision behavior. A collision behavior requires you to define the boundaries that the items in the animator will not be able to go past. For instance, if you define a line that runs from the bottom- left edge to the bottom-right edge of your reference view (the bottommost horizontal line of your reference view), and add a gravity behavior to your view as well, those views will be pulled down by gravity to the bottom of the view but will not be able to go further because they will collide with the bottom edge of the view, defined by the collision behavior.

If you want your reference view’s boundaries to be considered as the boundaries of your collision detection behavior, just set the translatesReferenceBoundsIntoBoundary property of the collision behavior’s instance to YES. If you want to add custom lines as boundaries to your collision behavior, simply use the addBoundaryWithIdentifi er:fromPoint:toPoint: instance method of the UICollisionBehavior class.

In this recipe, we are going to create two colored views, one on top of the other, and then add gravity to our animator so that the views will fall down from the center of the view controller’s view. Then we are going to add a collision behavior to the mix so that the views will not overlap each other. In addition, they won’t go outside the boundaries of the reference view (the view controller’s view).

So let’s begin by defining an array of our views and our animator:

#import "ViewController.h"

 

@interface ViewController ()


@property (nonatomic, strong) NSMutableArray *squareViews;

@property (nonatomic, strong) UIDynamicAnimator *animator;

@end

 

Then when our view appears on the screen, we will set up the collision and the gravity behaviors and add them to an animator:

-       (void)viewDidAppear:(BOOL)animated{

 [super viewDidAppear:animated];

        /* Create the views */

NSUInteger const NumberOfViews = 2;


self.squareViews = [[NSMutableArray alloc] initWithCapacity:NumberOfViews];

   NSArray *colors = @[[UIColor redColor], [UIColor greenColor]];

 

CGPoint currentCenterPoint = self.view.center;


CGSize eachViewSize = CGSizeMake(50.0f, 50.0f);

for (NSUInteger counter = 0; counter < NumberOfViews; counter++){

            UIView *newView =

            [[UIView alloc] initWithFrame:

             CGRectMake(0.0f, 0.0f, eachViewSize.width, eachViewSize.height)];

            newView.backgroundColor = colors[counter];

            newView.center = currentCenterPoint;

            currentCenterPoint.y += eachViewSize.height + 10.0f;

            [self.view addSubview:newView];

            [self.squareViews addObject:newView];

        }

        self.animator = [[UIDynamicAnimator alloc]

                         initWithReferenceView:self.view];

        /* Create gravity */

 UIGravityBehavior *gravity = [[UIGravityBehavior alloc]

initWithItems:self.squareViews];

 

        [self.animator addBehavior:gravity];

 

        /* Create collision detection */

        UICollisionBehavior *collision = [[UICollisionBehavior alloc]

                                          initWithItems:self.squareViews];

 

        collision.translatesReferenceBoundsIntoBoundary = YES;

 

        [self.animator addBehavior:collision];

}

转载于:https://www.cnblogs.com/ios-mark/p/3766423.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值