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 ...
随机推荐
- APP上传
原文网址: http://blog.csdn.net/ayangcool 前言:作为一名IOS开发者,把开发出来的App上传到App Store是必须的.下面就来详细介绍下具体流程. 1.打开苹果开发 ...
- 洛谷 P1908 逆序对(树状数组解法)
归并排序解法:https://www.cnblogs.com/lipeiyi520/p/10356882.html 题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不 ...
- 51nod 1562 玻璃切割 (set)
#include<stdio.h> #include<iostream> #include<set> using namespace std; typedef lo ...
- Spring+JCaptcha验证码使用示例
1,导入jcaptcha.jar包,这里用的是1.0版本 2,编写captcha-context.xml配置文件(非必须,可在spring配置文件中直接添加): <?xml version=&q ...
- Linux之文本处理命令
Sort 将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. -u 在输出行中去除重复行 -r 改为降序(默认升序) ...
- C51 笔记
一 关于宏常量的长度:C51中定义一个常数宏(默认是16位的),如果用宏表示一个32位的宏而不加'L'标志的话就会出错.如 #define BLOCK_A_BASEADDR 18*64*1024 / ...
- C#操作Windows用户
首先需要引入System.DirectoryServices.dll using System; using System.Collections.Generic; using System.Dire ...
- vue axios post不能本地json
vue 脚本架里axios post是不能本地json,GET可以 解决这个问题需要自己在node里写脚本: 在build里新建立fakedata.js var express = require(' ...
- JS排序--快速排序
用 JavaScript 实现快速排序代码如下: /* * @author liphong * @data 2019/02/24 */ var arr = []; // 需要被排序数组 /* * 分离 ...
- Vue.js - Day5 - Webpack
在网页中会引用哪些常见的静态资源? JS .js .jsx .coffee .ts(TypeScript 类 C# 语言) CSS .css .less .sass .scss Images .jpg ...