(转载)UIKIt力学教程
转载自:http://www.cocoachina.com/ios/20131226/7614.html
- UIView* square = [[UIView alloc] initWithFrame:
- CGRectMake(100, 100, 100, 100)];
- square.backgroundColor = [UIColor grayColor];
- [self.view addSubview:square];

- UIDynamicAnimator* _animator;
- UIGravityBehavior* _gravity;
- _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
- _gravity = [[UIGravityBehavior alloc] initWithItems:@[square]];
- [_animator addBehavior:_gravity];

- UICollisionBehavior* _collision;
- 在 viewDidLoad 末尾加入以下代码:
- _collision = [[UICollisionBehavior alloc]
- initWithItems:@[square]];
- _collision.translatesReferenceBoundsIntoBoundary = YES;
- [_animator addBehavior:_collision];

- UIView* barrier = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 130, 20)];
- barrier.backgroundColor = [UIColor redColor];
- [self.view addSubview:barrier];


- _collision = [[UICollisionBehavior alloc] initWithItems:@[square, barrier]];


- _collision = [[UICollisionBehavior alloc] initWithItems:@[square]];
- // add a boundary that coincides with the top edge
- CGPoint rightEdge = CGPointMake(barrier.frame.origin.x +
- barrier.frame.size.width, barrier.frame.origin.y);
- [_collision addBoundaryWithIdentifier:@"barrier"
- fromPoint:barrier.frame.origin
- toPoint:rightEdge];

- _collision.action = ^{
- NSLog(@"%@, %@",
- NSStringFromCGAffineTransform(square.transform),
- NSStringFromCGPoint(square.center));
- };
- 2013-07-26 08:21:58.698 DynamicsPlayground[17719:a0b] [1, 0, 0, 1, 0, 0], {150, 236}
- 2013-07-26 08:21:58.715 DynamicsPlayground[17719:a0b] [1, 0, 0, 1, 0, 0], {150, 243}
- 2013-07-26 08:21:58.732 DynamicsPlayground[17719:a0b] [1, 0, 0, 1, 0, 0], {150, 250}
- 2013-07-26 08:21:59.182 DynamicsPlayground[17719:a0b] [0.10679234, 0.99428135, -0.99428135, 0.10679234, 0, 0], {198, 325}
- 2013-07-26 08:21:59.198 DynamicsPlayground[17719:a0b] [0.051373702, 0.99867952, -0.99867952, 0.051373702, 0, 0], {199, 331}
- 2013-07-26 08:21:59.215 DynamicsPlayground[17719:a0b] [-0.0040036771, 0.99999201, -0.99999201, -0.0040036771, 0, 0], {201, 338}
- @protocol UIDynamicItem
- @property (nonatomic, readwrite) CGPoint center;
- @property (nonatomic, readonly) CGRect bounds;
- @property (nonatomic, readwrite) CGAffineTransform transform;
- @end
- @interface ViewController ()
- @end
- _collision.collisionDelegate = self;
- - (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id)item
- withBoundaryIdentifier:(id)identifier atPoint:(CGPoint)p {
- NSLog(@"Boundary contact occurred - %@", identifier);
- }
- 2013-07-26 08:44:37.473 DynamicsPlayground[18104:a0b] Boundary contact occurred - barrier
- 2013-07-26 08:44:37.689 DynamicsPlayground[18104:a0b] Boundary contact occurred - barrier
- 2013-07-26 08:44:38.256 DynamicsPlayground[18104:a0b] Boundary contact occurred - (null)
- 2013-07-26 08:44:38.372 DynamicsPlayground[18104:a0b] Boundary contact occurred - (null)
- 2013-07-26 08:44:38.455 DynamicsPlayground[18104:a0b] Boundary contact occurred - (null)
- 2013-07-26 08:44:38.489 DynamicsPlayground[18104:a0b] Boundary contact occurred - (null)
- 2013-07-26 08:44:38.540 DynamicsPlayground[18104:a0b] Boundary contact occurred - (null)
- UIView* view = (UIView*)item;
- view.backgroundColor = [UIColor yellowColor];
- [UIView animateWithDuration:0.3 animations:^{
- view.backgroundColor = [UIColor grayColor];
- }];

- UIDynamicItemBehavior* itemBehaviour = [[UIDynamicItemBehavior alloc] initWithItems:@[square]];
- itemBehaviour.elasticity = 0.6;
- [_animator addBehavior:itemBehaviour];

- BOOL _firstContact;
- if (!_firstContact)
- {
- _firstContact = YES;
- UIView* square = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 100, 100)];
- square.backgroundColor = [UIColor grayColor];
- [self.view addSubview:square];
- [_collision addItem:square];
- [_gravity addItem:square];
- UIAttachmentBehavior* attach = [[UIAttachmentBehavior alloc] initWithItem:view
- attachedToItem:square];
- [_animator addBehavior:attach];
- }


(转载)UIKIt力学教程的更多相关文章
- UIKit 力学教程
import UIKit class ViewController: UIViewController { var animator: UIDynamicAnimator? override func ...
- 转载 CSS3 经典教程系列:CSS3 盒阴影(box-shadow)详解
目标大纲 文章转载 CSS3 经典教程系列:CSS3 盒阴影(box-shadow)详解 IE中CSS-filter滤镜小知识大全 CSS实现跨浏览器兼容性的盒阴影效果
- 【转载】NSURLSession教程
原文:http://www.raywenderlich.com/51127/nsurlsession-tutorial 查理·富尔顿 2013年10月9日, 推特 注意从雷 :这是一个缩写版的一章 i ...
- [转载]Ocelot简易教程(六)之重写配置文件存储方式并优化响应数据
作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9807125.html 很多人都说配置文件的配置很繁琐,如果存储在数据库就方便很多,可以通过自定义UI界面 ...
- [转载]Ocelot简易教程(五)之集成IdentityServer认证以及授权
作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9807125.html 最近比较懒,所以隔了N天才来继续更新第五篇Ocelot简易教程,本篇教程会先简单介 ...
- [转载]Ocelot简易教程(三)之主要特性及路由详解
上篇<Ocelot简易教程(二)之快速开始2>教大家如何快速跑起来一个ocelot实例项目,也只是简单的对Ocelot进行了配置,这篇文章会给大家详细的介绍一下Ocelot的配置信息.希望 ...
- [转载]Ocelot简易教程(一)Ocelot是什么
Ocelot简易教程(一)Ocelot是什么 简单的说Ocelot是一个用.NET Core实现并且开源的API网关技术. 可能你又要问了,什么是API网关技术呢?Ocelot又有什么特别呢?我们又该 ...
- iOS之UIKit系列教程<一>
前言:博主接触iOS的编程也有一段时间,今天把有关UI控件的一些知识在这里做一些总结. 申明:此系列文章都是使用目前最新版本swift3.0.1进行讲解的,与其他版本可能略有差异. 一,UIKit之设 ...
- [转载]SVN使用教程
SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...
随机推荐
- 这次OpenSSL HeartBleed漏洞是怎么一回事呢?
“心脏出血”(Heartbleed)被称为互联网史上最严重的安全漏洞之一,波及了大量常用网站.服务,包括很多人每天都在用的 Gmail 等等,可能导致用户的密码.信用卡轻易泄露.但是我们可能对它还不是 ...
- HDU 1556 线段树/树状数组/区间更新姿势 三种方法处理
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 【BZOJ3132】上帝造题的七分钟 [树状数组]
上帝造题的七分钟 Time Limit: 20 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description “第一分钟,X说,要有矩阵 ...
- bzoj3127/3697 [Usaco2013 Open]Yin and Yang
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3127 http://www.lydsy.com/JudgeOnline/problem.ph ...
- 10款最新CSS3/jQuery菜单导航插件
这是我们在2014年收集的10款最新的CSS3 / jQuery菜单导航插件,不论从外观样式,还是功能扩展性,这些jQuery菜单一定可以满足大家的设计需求.这次我们收集的jQuery菜单,有水平 菜 ...
- Python 数据库连接池DButils
常规的数据库链接存在的问题: 场景一: 缺点:每次请求反复创建数据库连接,连接数太多 import pymysql def index(): conn = pymysql.connect() curs ...
- 取Session数据语句在应放在哪里
UsersModel loginUser = (UsersModel)Session["usersModel"]; 直接放在 public partial class PagesF ...
- 移植WordPress到Ubuntu16.04
移植WordPress到Ubuntu16.04 新建 模板 小书匠 移植WordPress到Ubuntu16.04 搭建好LAMP环境后,可以按照以下方法,将本地站点移植到服务器上. 以WordPre ...
- Selenium2+python自动化74-jquery定位【转载】
转至博客:上海-悠悠 前言 元素定位可以说是学自动化的小伙伴遇到的一道门槛,学会了定位也就打通了任督二脉,前面分享过selenium的18般武艺,再加上五种js的定位大法. 这些还不够的话,今天再分享 ...
- python的算法:二分法查找(2)--bisect模块
Python 有一个 bisect 模块,用于维护有序列表.bisect 模块实现了一个算法用于插入元素到有序列表.在一些情况下,这比反复排序列表或构造一个大的列表再排序的效率更高.Bisect 是二 ...