一、协议(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的更多相关文章

  1. CS193p Lecture 9 - Animation, Autolayout

    Animation(动画) Demo Dropit续 Autolayout(自动布局) 三种添加自动布局的方法: 使用蓝色辅助虚线,右键选择建议约束(Reset to Suggested Constr ...

  2. CS193p Lecture 11 - UITableView, iPad

    UITableView 的 dataSource 和 delegate dataSource 是一种协议,由 UITableView 实现,将 Model 的数据给到 UITableView: del ...

  3. CS193p Lecture 10 - Multithreating, UIScrollView

    Multithreating(多线程) 网络请求例子: NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithStrin ...

  4. CS193p Lecture 7 - Views, Gestures

    Views 如何绘制自定义图像 Gestures 如何处理用户手势操作 Views 1.它是基本的构造块,代表屏幕上一块矩形区域,定义了一个坐标空间,在此空间中可以绘制,可以添加触控事件: 2.它是分 ...

  5. CS193p Lecture 6 - UINavigation, UITabBar

    抽象类(Abstract):指的是这个类不能被实例化,只能被继承: OC中没有关键词来标明某个类是抽象类,只能在注释中标注一下: 抽象类中的抽象方法,必须是public的,使方法称为public的方法 ...

  6. CS193p Lecture 5 - View Controller Lifecycle

    1. UITextView @property(nonatomic,readonly,retain) NSTextStorage *textStorage 是 NSMutableAttributedS ...

  7. CS193p Lecture 4 - Foundation, Attributed Strings

    消息机制 调用一个实例(instance)的方法(method),就是向该实例的指针发送消息(message),实例收到消息后,从自身的实现(implementation)中寻找响应这条消息的方法. ...

  8. [C2P3] Andrew Ng - Machine Learning

    ##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...

  9. Framework for Graphics Animation and Compositing Operations

    FIELD OF THE DISCLOSURE The subject matter of the present disclosure relates to a framework for hand ...

随机推荐

  1. 3DMAX 7 角色建模1 人头建模

    说明: mesh与poly 可编辑多边形是一个多边形网格:即与可编辑网格不同,其使用超过三面的多边形.可编辑多边形非常有用,因为它们可以避免看不到边缘.例如,如果您对可编辑多边形执行切割和切片操作,程 ...

  2. 五分钟了解Mecanim角色动画系统

    http://www.narkii.com/club/thread-305414-1.html Unity 4.0推出的Mecanim动画系统已经有一段时间,不过据了解很多的朋友仍然在使用原来的角色动 ...

  3. Codevs 1569 最佳绿草

    1569 最佳绿草  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 贝茜正计划着这一天如何美美地咀嚼春天的绿 ...

  4. Sublime Text 报“Pylinter could not automatically determined the path to lint.py

    Pylinter could not automatically determined the path to lint.py. please provide one in the settings ...

  5. CSS之html元素与body元素的范围

  6. laravel-admin 配置富文本编辑器流程

    laravel-admin默认去除富文本编辑器的,官方也给出了配置方法. 我配置的是wangEditor,本来配置完后就能愉快得使用了,可万万没想到还是有坑的.默认是用base64上传的,也就是数据库 ...

  7. Redis安装(centOS7)

    1.安装gcc环境 sudo yum install gcc-c++ 2.安装wget sudo yum install wget 3.下载Redis源码包 wget http://download. ...

  8. python入门之递归

    表现形式: 函数体里包含执行本身 def f1(): r = f1() f1() 实例: 斐波那契数 (a1+a2=a3   a2+a3=a4   a3+a4=a5   ......) def f1( ...

  9. Executor等系列概念介绍

    这里对几个常见的的名词进行介绍 Executor 这是个接口,只声明了一个方法—— public interface Executor { void execute(Runnable command) ...

  10. Linux--NiaoGe-Service-04

    操作系统版本:CentOS 6.10 x86_64 查看内核所获取到的网卡信息 [root@xueji ~]# dmesg | grep -in eth :e1000 ::-bit) :0c::6b: ...