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

}

Detecting and reacting to collisions Between UI
Components

时间: 2024-10-08 08:34:42

Detecting and reacting to collisions Between UI Components的相关文章

Adding Gravity to your UI Components

Problem You want your UI components to have gravity, so that if they are dragged up to the top of the screen, they will descend on their own. Combining this with the collision behavior that you will learn later, you can create UI components that fall

Magento2 UI components概述

来源:宝鸡SEO UI components 概述Magento UI components 是用来展示不同的UI元素,比如表,按钮,对话框等.他们被用于简单灵活的交互界面渲染.Components被用来渲染结果界面,提供进一步的与javascript组件和服务器的交互.Magento UI components被实现为一个标准的模块叫Magento_UI.想要在你的模块里面使用UI Components,你需要在你的composer.json文件里面添加对Magento_UI的依赖.以下XSD

免费素材:25套免费的 Web UI 设计的界面元素(转)

Web 元素是任何网站相关项目都需要的,质量和良好设计的元素对于设计师来说就像宝贝一样.如果您正在为您的网站,博客,Web 应用程序或移动应用程序寻找完美设计的网页元素,那么下面这个列表会是你需要的. 今天这里收集的免费资源是25套免费的 Web UI 设计的界面元素,用于和Mobile App设计.它包括所有的设计元素的 PSD 格式,某些元素提供矢量格式,可以缩放到任意大小. UI Kit – Free PSD Download Ui Kit [Free PSD] Download Free

4. Building a Dynamic UI with Fragments 使用片段构建动态UI

To create a dynamic and multi-pane user interface on Android, you need to encapsulate UI components and activity behaviors into modules that you can swap into and out of your activities. You can create these modules with the Fragment class, which beh

[React] Recompose: Theme React Components Live with Context

SASS Bootstrap allows us to configure theme or branding variables that affect all components (e.g. Primary Color or Link Color). When we isolate our styles inside React components we lose the ability to theme them. To get round this we can put our th

Architecture Components

1.Why "Architecture" Components? 2.what does architecture components include? { Room //a robust SQL object mapping library ViewModel //provide data for UI components and survive configuration changes LiveData  //monitor changes,database observer

Android学习路线(二十三)运用Fragment构建动态UI——Fragment间通讯

先占个位置,下次翻译 :p In order to reuse the Fragment UI components, you should build each as a completely self-contained, modular component that defines its own layout and behavior. Once you have defined these reusable Fragments, you can associate them with

Hybrid UI framework shootout: Ionic vs. Famo.us vs. F7 vs. OnsenUI

1 Introduction In the past 2 years I’ve been working intensively on mobile applications, mostly hybrid, and mostly with AngularJS. In my quest to find a good UI framework that integrates with AngularJS, I came across the following options: IonicFrame

Android React Native使用原生UI组件

Android React Native 已经将几个常用的原生组件进行了封装,比如 ScrollView 和 TextInput,但是并不是所有系统的原始组件都被封装了,因此有的时候我们不得不自己动手封装一下,从而能够使用那些React Native没有为我们封装的原生组件,比如WebView,官方并没有提供Android端的实现,那么我们现在就动手封装一下WebView. 之前写过一篇文章Android React Native使用原生模块,而使用原生UI组件的方法和使用原生模块的方法十分类似