(转载)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是什么? ...
随机推荐
- 在iis上部署ssl证书 https
1.取走证书下载下来的文件.解压iis的压缩包. 2.打开internet信息服务iis管理器 3.双击打开后,选择导入,导入我们刚刚解压得到的pfx文件,这个pfx文件就是你需要部署域名的那个文件. ...
- CodeIgniter自带的数据库类使用介绍
在 CodeIgniter 中,使用数据库是非常频繁的事情.你可以使用框架自带的数据库类,就能便捷地进行数据库操作. 初始化数据库类 依据你的数据库配置载入并初始化数据库类: view source ...
- n元线性方程非负整数解的个数问题
设方程x1+x2+x3+...+xn = m(m是常数) 这个方程的非负整数解的个数有(m+n-1)!/((n-1)!m!),也就是C(n+m-1,m). 具体解释就是m个1和n-1个0做重集的全排列 ...
- codeforces902C. Hashing Trees
https://codeforces.com/contest/902/problem/C 题意: 给你树的深度和树的每个节点的深度,问你是否有重构,如果有重构输出两个不同的结构 题解: 如果相邻节点的 ...
- charles 踩坑记录
charles破解教程:http://www.jianshu.com/p/12e75eb8f53d 1.需注意软件和破解脚本的版本是否正确(例如3.x.x版本的破解脚本不能用于4.x.x版本的char ...
- 【洛谷 P2346】四子连棋(状态压缩,搜索)
其实这题可以直接二进制状压做,1表示黑棋,0表示白棋,另外记录下2个空点的位置就行了. 具体看代码(冗长): #include <iostream> #include <cstdio ...
- python Fielddata is disabled on text fields
# 执行https://www.elastic.co/guide/cn/elasticsearch/guide/current/_aggregation_test_drive.html中的例子时报错F ...
- 一道面试题:C++相比C#或者java的优势到底在哪里
被问到了这样一道面试题,当时就懵了,内心一直觉得C++肯定在很多方面要比C#或者java要牛b的. 但是真的不知道怎么回答. 问题是:你以前一直做得是.NET相关项目,现在为什么找C++开发相关工作呢 ...
- UVA 10668 Expanding Rods
Problem A: Expanding Rods When a thin rod of length L is heated n degrees, it expands to a new lengt ...
- Python多线程常用包对比
python由于本身的特质,不能实现真正的多核并行运算,但是有一些第三方库较好地模拟了在多核环境下的并行运算,例如pp包以及multiprocessing,那么哪种更能充分利用多核心呢? 这里我简单做 ...