1. //
  2. // TouchView.h
  3. // LessonUIControlAndSubClass
  4.  
  5. #import <UIKit/UIKit.h>
  6.  
  7. @class TouchView;
  8. //1.制定协议,协议名字格式:类名+Delegate
  9. @protocol TouchViewDelegate <NSObject>
  10.  
  11. @optional
  12. - (void)touchBegan:(TouchView *)touchView;
  13. - (void)touchMoved:(TouchView *)touchView;
  14. - (void)touchEnded:(TouchView *)touchView;
  15. - (void)touchCancelled:(TouchView *)touchView;
  16.  
  17. @end
  18.  
  19. @interface TouchView : UIView
  20.  
  21. //2.写属性,属性名delegate,类型是id,并且要遵守协议<类名Delegate>
  22. @property (assign, nonatomic) id<TouchViewDelegate> delegate;
  23.  
  24. @end

  1. //
  2. // TouchView.m
  3. // LessonUIControlAndSubClass
  4. //
  5.  
  6. #import "TouchView.h"
  7.  
  8. @implementation TouchView
  9.  
  10. //3.一旦找到代理,让代理执行事情
  11. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  12. //判断delegate是否实现了某方法
  13. if ([_delegate respondsToSelector:@selector(touchBegan:)]) {
  14. [_delegate touchBegan:self];
  15. }
  16. }
  17.  
  18. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  19. if ([_delegate respondsToSelector:@selector(touchMoved:)]) {
  20. [_delegate touchMoved:self];
  21. }
  22. }
  23.  
  24. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  25. if ([_delegate respondsToSelector:@selector(touchEnded:)]) {
  26. [_delegate touchEnded:self];
  27. }
  28. }
  29.  
  30. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  31. if ([_delegate respondsToSelector:@selector(touchCancelled:)]) {
  32. [_delegate touchCancelled:self];
  33. }
  34. }
  35.  
  36. @end

protocol的简单写法的更多相关文章

  1. 使用Ext.Net时,配置文件的最简单写法

    使用Ext.Net时,配置文件的最简单写法 <?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配 ...

  2. html5滚动页面简单写法

    html5滚动页面简单写法纵向滚动比较简单 直接在外面加个高度 然后overflow-y: auto; 横向比较复杂了外面写两层 最外面一层写个宽度 overflow-x: auto;第二层 写wid ...

  3. TFTP(Trivial File Transfer Protocol,简单文件传输协议)

    TFTP(Trivial File Transfer Protocol,简单文件传输协议),是 TCP/IP 协议族中用来在客户机和服务器之间进行简单文件传输的协议,开销很小.这时候有人可能会纳闷,既 ...

  4. PHP去重的简单写法

    PHP去重的简单写法用array_flip实现去重效果 <pre><?php$arr =array("a"=>"a1","b& ...

  5. Google Protocol Buffer 简单介绍

    以下内容主要整理自官方文档. 为什么使用 Protocol Buffers .proto文件 Protocol Buffers 语法 编译.proto文件 Protocol Buffers API 枚 ...

  6. linux IPC socket(3)server简单写法

    写server的一些流程总结 一.向内核申请一个socket TCP形式 sock_fd = socket(AF_INET, SOCK_STREAM, ); UDP形式 sfd = socket(AF ...

  7. foreach循环的简单写法

    简单的foreach循环写法: pickedItems.ForEach(item => { this.List.Remove(item); }); //注意,List 必须和pickedItem ...

  8. javascript模块简单写法

    写法1: (function (wd, doc) { var mw = {}; mw.noConflict = noConflict; var _$ = wd.$; wd.$ = mw; functi ...

  9. WPF之Binding的三种简单写法

    环境 类代码 public class Person:INotifyPropertyChanged { private string name; public string Name { get { ...

随机推荐

  1. iOS全军覆没!分分钟教你徒手破解iPhone

    http://iphone.tgbus.com/news/class/201503/20150304141407.shtml 破解id密码的最新dns 218.59.181.182

  2. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  3. 函数式functor的理解

    // 参考 // http://jiyinyiyong.github.io/monads-in-pictures/ // https://llh911001.gitbooks.io/mostly-ad ...

  4. hdu-5525 Product(费马小定理)

    题目来源:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=644&pid=1003 前面用奇偶性约掉2, ...

  5. Maven打包程序

    1.src/main目录下建立scritps.config.assembly目录 scritps:存放脚本文件如批处理 assembly:打包配置文件   2.assembly目录下建立package ...

  6. OpenCV 计算区域的内部参数

    对于一个区域,怎么进一步针对区域内部特征进行处理呢 ? 首先,我们要提取出来内部的某些特征才能说话,下面提取一些简单的特征,话不多说见代码: 1.平均数及方差参数: Mat tempMean, tem ...

  7. SAP公司间采购订单关联交货单报表源代码(自己收藏)

    SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF } ...

  8. BIND的进阶二:视图,日志,转发,子域的授权

    实验分为4部分组成: 1:DNS的转发   2:DNS日志     3:子域的授权 4:智能DNS的简单配置根据网段来分配不同的ip地址 一:DNS的转发: 转发方式有两种:only (直接把客户端请 ...

  9. Java开发常用的在线工具

    原文出处: hollischuang(@Hollis_Chuang) 作为一个Java开发人员,经常要和各种各样的工具打交道,除了我们常用的IDE工具以外,其实还有很多工具是我们在日常开发及学习过程中 ...

  10. cin, cin.getline等函数

    char s[100]; cin>>s;         // 输入一个字符串,遇“空格”.“TAB”.“回车”都结束 cin.getline(s, 20);    // cin.get( ...