delegate
来源:互联网 发布:主力建仓优化指标 编辑:程序博客网 时间:2024/06/05 14:12
#import <UIKit/UIKit.h>// 1.声明协议@protocol MyButtonDelegate <NSObject>- (void)changeColor;@end@interface MyButton:UIView// 2.声明代理人的属性@property(nonatomic, assign)id<MyButtonDelegate>delegate;@end
#import "MyButton.h"@implementation MyButton// 3.通过touch方法,来设置代理人执行的方法- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.delegate changeColor]; }@end
#import "RootViewController.h"#import "MyButton.h"// 4.签协议@interface RootViewController ()<MyButtonDelegate>@end@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor yellowColor];// // 创建一个mybutton对象 MyButton *button = [[MyButton alloc] initWithFrame:CGRectMake(100, 100, 150, 50)]; button.backgroundColor = [UIColor yellowColor]; [self.view addSubview:button]; [button release];//// // 5.设置代理人 button.delegate = self; UIImage *image = [UIImage imageNamed:@"/Users/dllo/Desktop/UI/UI文件/UI04/UI04_delegate/UI04_delegate/岛屿0.jpg"]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; // 给imageView设置图片 imageView.image = image; [self.view addSubview:imageView]; [imageView release]; imageView.layer.cornerRadius = 100; imageView.layer.borderWidth = 1; imageView.layer.masksToBounds = YES; // 毛玻璃效果 UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIblurEffectStyleLight]; // iOS8.0之后出现的新效果,用来显示模糊效果 UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect]; effectView.frame = CGRectMake(0, 0, imageView.frame.size.width, iamgeView.frame.size.height); effectView.alpha = 0.5; // 添加子视图 [imageView addSubview:effectView];}// 6.实现协议方法- (void)changeColor { // 设置随机的背景颜色 self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1]; }
0 0
- delegate
- delegate
- Delegate
- delegate
- Delegate
- Delegate
- Delegate
- Delegate
- delegate
- Delegate
- Delegate
- Delegate
- Delegate
- delegate
- Delegate
- delegate
- delegate
- delegate
- 基于IBM Bluemix部署Java Web项目实战演练
- Drawable和Bitmap相互转换
- Android 基本知识(一)
- C++标准模板库 迭代器 iterator 详解(二)
- poj2151
- delegate
- 从零开始学C++之STL(一):STL六大组件简介
- 负数取模
- hiho刷题日记——第十三天最近公共祖先·一
- 淘宝SKU组合查询算法实现
- Metasploit 实战第2章 第二节
- 读取HashMap尤其是比较复杂结构时Entry报错
- C++STL之迭代器
- cf(405A,B,C,D)