OC Protocol

OC Protocol

1. protocol 基本概念

  • protocol 翻译为 协议
  • 在Java中有接客interface的概念, 接口就是一堆方法的什么没有实现,而在的interface是一个类的头文件的声明,并不是真正意义的接口的意思,在OC中是有一个叫protocol来实现的。
  • protocol 可以用来声明一些必须实现或选择实现的方法,这个和Java中是完全不同的。
  • protocol 的作用
    • 用来什么一些方法
    • 也就是说一个protocol是有一系列方法的声明来实现的。

2. protocol 语法格式

  • Protocol 的定义

    @protocol 协议名称
    

// 方法声明列表
@end

  • 用类去遵守协议protocol
    • 任何类只要遵守了协议protocol就相当于有了protocol所有方法的声明。
    • 一个类可以遵守一个或者多个协议。

      @interface 类名:父类 <协议名称1,协议名称2>
      @end

    • 协议也可以继承协议!

3. protocol 和 继承的区别

  • 继承之后就默认有了实现,而protocol 只有声明没有实现
  • 相同类型的类中可以使用继承,比如老师学生都可以继承自人。但是不同类型的类只能使用协议Protocol。
  • protocol可以用于存储方法中的什么,将多个类共同的方法抽取出来,以后让这些类去遵守协议即可。

4. protocol 使用注意

  1. protocol 就一个用途,就是用来存放一堆方法的声明,不能写实现,也不能声明属性、成员变量。
  2. 只要父类遵守了协议,子类就也遵守。
  3. OC中不支持多继承类, 但是可以继承多个协议,尖括号内用逗号隔开。继承是冒号:
  4. 协议可以遵守协议, 只要B协议遵守A协议, 遵守B协议的类C 就同时拥有了AB协议的所有方法声明。

创建一个类建议继承 NSObject
创建一个协议建议遵守 @protocol < NSObject >;

5. 基协议

  • NSObject 是一个基类 ,最基本最根本的类,所有的类最终都是继承NSObject ;
  • @protocol < NSObject > 是基协议,是最基本最根本的协议。
  • 建议每个协议都遵守 @protocol < NSObject > 协议。

6. @required @optional 两个关键字的使用

  • @required 是必须实现的方法。
  • @optional 是必须实现的方法。
  • 如果没有任何关键字修饰的方法,默认情况就是@required 必须实现的。如果类中没实现该方法,就会警告缺少该方法!
  • @optional 没有实现不会警告
  • 作用范围 出现开始 到 下一个关键字为止,用于程序猿交流。

7. 使用场景

  1. 注意 变量赋值的时候 ! // 创建wife的时候直接在wife右边要求满足协议。 协议使用第一个应用场景,可以将协议写在数据类型的右边,明确表示如果要给该变量赋值, 必须要该对象遵守这个协议
  2. 注意 成员属性定义时 可以写入协议//类的声明中! 注意 类型限定一定是写在类型数据的右边!!!
  3. 注意 虽然接收了某一个对象的时候,对这个对象进行了类型限定(限定其必须实现某个协议),但是并不是意味着对象就真的实现了协议里的方法。所以要在调用对象的协议方法时候进行一次验证。防止光说不做 赖皮!
 if ([self.wife respondsToSelector(cooking)])
 {
 [self.wife cooking];
 }
  • 设定场景
  • 某程序猿要找一个媳妇 ,要求会做饭,会洗衣服,有国企工作的漂亮女生。
  • 满足条件的女生都可以向他发送消息。
@protocol wifeCondtion  < NSObject >
-(void)cooking;
-(void)washing;
-(void)job;
@end
//协议使用第一个应用场景,可以将协议写在数据类型的右边,明确表示如果要给该变量赋值, 必须要该对象遵守这个协议
wife < wifeCondtion > * wifeName =[[ wife alloc]init];

@interface Person : NSObject
//注意  类型限定一定是写在类型数据的右边!!!
@property (nonatomic,strong)Wife < WifeCondtion> * wife;
@end

8. 代理设计模式

* 引申 @private @protected @public

指令含义
@private作用范围只能在自身类
@protected作用范围在自身类和继承自己的子类,什么都不写,默认是此属性。
@public作用范围最大,在任何地方

2015-04-03 21:36:19 Edit with Markdown © Yun.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 Objective-C 编写的手势解锁密码的示例代码: 首先,在你的项目中创建一个名为 "GestureLockView" 的 UIView 子类。这个类将用于绘制手势解锁的视图。 GestureLockView.h: ```objective-c #import <UIKit/UIKit.h> @protocol GestureLockViewDelegate <NSObject> - (void)gestureLockView:(UIView *)lockView didFinishWithPath:(NSString *)path; @end @interface GestureLockView : UIView @property (nonatomic, weak) id<GestureLockViewDelegate> delegate; @end ``` GestureLockView.m: ```objective-c #import "GestureLockView.h" @interface GestureLockView() @property (nonatomic, strong) NSMutableArray *selectedButtons; @end @implementation GestureLockView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; self.selectedButtons = [NSMutableArray array]; } return self; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); for (int i = 0; i < 9; i++) { CGFloat x = (i % 3) * 100 + 25; CGFloat y = (i / 3) * 100 + 25; CGRect frame = CGRectMake(x, y, 50, 50); UIButton *button = [[UIButton alloc] initWithFrame:frame]; button.tag = i + 1; button.layer.cornerRadius = 25.0; button.layer.borderWidth = 1.0; button.layer.borderColor = [UIColor blackColor].CGColor; [button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateSelected]; [button setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; } if (self.selectedButtons.count > 0) { UIBezierPath *path = [UIBezierPath bezierPath]; for (int i = 0; i < self.selectedButtons.count; i++) { UIButton *button = self.selectedButtons[i]; CGPoint point = button.center; if (i == 0) { [path moveToPoint:point]; } else { [path addLineToPoint:point]; } } [path addLineToPoint:self.currentPoint]; [[UIColor blueColor] set]; path.lineWidth = 5.0; CGContextAddPath(context, path.CGPath); CGContextStrokePath(context); } } - (void)buttonTapped:(UIButton *)button { button.selected = YES; [self.selectedButtons addObject:button]; [self setNeedsDisplay]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self handleTouches:touches]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self handleTouches:touches]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSMutableString *path = [NSMutableString string]; for (UIButton *button in self.selectedButtons) { [path appendFormat:@"%ld", button.tag]; } [self.delegate gestureLockView:self didFinishWithPath:path]; [self reset]; } - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self reset]; } - (void)handleTouches:(NSSet<UITouch *> *)touches { UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self]; for (UIButton *button in self.subviews) { if (CGRectContainsPoint(button.frame, point)) { if (![self.selectedButtons containsObject:button]) { button.selected = YES; [self.selectedButtons addObject:button]; } break; } } self.currentPoint = point; [self setNeedsDisplay]; } - (void)reset { for (UIButton *button in self.selectedButtons) { button.selected = NO; } [self.selectedButtons removeAllObjects]; [self setNeedsDisplay]; } - (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } @end ``` 在你的视图控制器中,你可以添加一个 GestureLockView 实例,并设置它的 delegate。当手势解锁完成时,GestureLockView 将调用代理方法 gestureLockView:didFinishWithPath:,你可以在这个方法中处理解锁的逻辑。 ViewController.h: ```objective-c #import <UIKit/UIKit.h> #import "GestureLockView.h" @interface ViewController : UIViewController <GestureLockViewDelegate> @end ``` ViewController.m: ```objective-c #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) GestureLockView *lockView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.lockView = [[GestureLockView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)]; self.lockView.center = self.view.center; self.lockView.delegate = self; [self.view addSubview:self.lockView]; } - (void)gestureLockView:(UIView *)lockView didFinishWithPath:(NSString *)path { NSLog(@"Path: %@", path); if ([path isEqualToString:@"123"]) { NSLog(@"Unlock success!"); } else { NSLog(@"Unlock failed!"); } } @end ``` 这里的解锁密码是 "123"。你可以根据需要修改这个密码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值