CS193p Lecture 8 - Protocols, Blocks and Animation
一、协议(Protocols)
1. 声明协议
@protocol Foo <Xyzzy, NSObject>
// ...
@optinal
//
@required
//...
@end
(与@interface几乎一致)
-协议只是方法的声明,没有实现部分
-协议中声明的方法必须实现
加上@optional,其后的方法可选,其前的方法必须实现
加上@required,其后的方法必须实现
-如果你要实现协议Foo,也要实现Xyzzy协议和NSObject中全部必须的方法,
2. 指定协议
// 公有指定
#import "Foo.h"
@interface MyClass : NSObject <Foo>
// ...
@end // 私有指定
@interface MyClass() <Foo>
@end
@implementation MyClass
// ...
@end
3.用协议指定变量
id <Foo> obj = [[MyClass alloc] init]
4. 协议的使用场景
最常见的是:delegate 和 dataSource
视图向控制器发消息:通过 不可视结构化通信(blind structured communication)
@property (nomatomic, weak) id <UISomeObjectDelegate> delegate;
@property (nomatomic, weak) id <UISomeObjectDataSource> dataSource;
二、块(Blocks)
1. 在其他语言中,称为闭包(closure)
2. 加上__block后,变量在块中保存的不在是变量的值,而是变量的地址,所以可以被修改;
如果加上这个修饰,此变量会从栈移到堆中,从而可以在block中使用,等block结束,再将信息复制回堆,再放回到栈上;
3. 每次向block中的对象发送消息时,系统都会创建一个指向该对象的强指针,直至block不存在
4. block表现得像对象,体现在:
- 可以被存储,如存在property、dictionary、array中
- 可以被自动引用计数(ARC)统计
- 能接受的一个消息,就是复制(copy)
5. Memory cycle
解决办法:
创建一个weak版的self,然后block中用weakSelf
__weak MyClass *weakSelf = self;
[self.myBlocks addObject:^{
[weakSelf doSomeThing];
}]
6. block的用处
三、动画(Animation)
1、UIView Animation
frame、transform、alpha
+ (void)animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
+ (void)transitionWithView:(UIView *)view
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
2、Dynamic Animation
UIDynamicAnimator
UIDynamicBehavior
UIGravityBehavior(重力)
UICollisionBehavior(碰撞)
UIAttachmentBehavior(吸附)
UISnapBehavior(捕捉)
UIPushBehavior(推动)
UIDynamicItem
Demo
Dropit(类似俄罗斯方块的小游戏)
CS193p Lecture 8 - Protocols, Blocks and Animation的更多相关文章
- CS193p Lecture 9 - Animation, Autolayout
Animation(动画) Demo Dropit续 Autolayout(自动布局) 三种添加自动布局的方法: 使用蓝色辅助虚线,右键选择建议约束(Reset to Suggested Constr ...
- CS193p Lecture 11 - UITableView, iPad
UITableView 的 dataSource 和 delegate dataSource 是一种协议,由 UITableView 实现,将 Model 的数据给到 UITableView: del ...
- CS193p Lecture 10 - Multithreating, UIScrollView
Multithreating(多线程) 网络请求例子: NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithStrin ...
- CS193p Lecture 7 - Views, Gestures
Views 如何绘制自定义图像 Gestures 如何处理用户手势操作 Views 1.它是基本的构造块,代表屏幕上一块矩形区域,定义了一个坐标空间,在此空间中可以绘制,可以添加触控事件: 2.它是分 ...
- CS193p Lecture 6 - UINavigation, UITabBar
抽象类(Abstract):指的是这个类不能被实例化,只能被继承: OC中没有关键词来标明某个类是抽象类,只能在注释中标注一下: 抽象类中的抽象方法,必须是public的,使方法称为public的方法 ...
- CS193p Lecture 5 - View Controller Lifecycle
1. UITextView @property(nonatomic,readonly,retain) NSTextStorage *textStorage 是 NSMutableAttributedS ...
- CS193p Lecture 4 - Foundation, Attributed Strings
消息机制 调用一个实例(instance)的方法(method),就是向该实例的指针发送消息(message),实例收到消息后,从自身的实现(implementation)中寻找响应这条消息的方法. ...
- [C2P3] Andrew Ng - Machine Learning
##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...
- Framework for Graphics Animation and Compositing Operations
FIELD OF THE DISCLOSURE The subject matter of the present disclosure relates to a framework for hand ...
随机推荐
- Unity3D研究院之游戏对象的访问绘制线与绘制面详解(十七)
一眨眼学习Unity3D 也有一段时间了,基本已经拿下了这套游戏引擎,回过头来想想以前写的RPG 游戏引擎,越来越发现以前写的就是垃圾.人果然是要不断学习与不断进步,好好学习,天天向上.哇咔咔- 加油 ...
- [Xcode 实际操作]八、网络与多线程-(24)社会化分享功能开发包的安装和配置:微信、QQ、微博
目录:[Swift]Xcode实际操作 本文将演示如何在开放平台注册应用,并获得相关的密钥信息,用于实现后面文章的微博分享功能. 一.新浪微博开放平台 [登录]->[微服务]->[粉丝服务 ...
- js框架:jQuery
· jQuery是一个轻量级的“写的少,做的多”的JavaScript函数库(jQuery版本2以上不支持IE6,7,8) · jQuery 的功能概括: 1.html 的元素选取 2.html的元素 ...
- STP-2-三个选择
1.选择根交换机 2.确定根端口 3.确定指定端口 1.选择根交换机 stp中只有一台交换机能成为根(Root),每台交换机按自己的STP逻辑,先发一个hello称自己为根,如果收到了比自 ...
- DRF教程4-视图集和路由类
rest框架包括一个处理viewset的抽象,允许开发人员集中精力处理api交互和建模,url构造都根据常见方式自动处理. ViewSet类 几乎和VIew类一样,不过它提供read,update这样 ...
- Codeforces 1139F(树状数组+扫描线)
题目传送 做法 对于每个人,inc为x,pref为y:对于每道菜,p和s为x,b为y 于是根据题意有\[p[i]<=x<=s[i]\]\[p[i]+b[i]<=x+y\]\[p[i] ...
- GYM 101673E(暴搜预处理)
1.不会超过500个不同的串-- 2.样例没给has到has是怎么样的,实测是true. 3.记忆化别剪错枝就好,嘤嘤嘤-- const int maxn = 505 + 5; int n, m, t ...
- 在spring的过滤器中注入实体类(@autowire会失效可使用这个方法)
转载:难得可贵的好文章 https://blog.csdn.net/chl191623691/article/details/78657638 首先,本文 绝对是好文!不止本文,作者的文章都是很经 ...
- asp.net,监听输入框值的即时变化onpropertychange、oninput
作者:自由天堂发布站点:WEB六零零 网页设计制作原文地址:http://www.web600.net/html/editor/JavaScript/201001131529.html 要达到的效果 ...
- asp.net中,跳转页面的几种方式
js方式的页面跳转1.window.location.href方式 <script language="javascript" type="text/java ...