⼀、target/action设计模式
耦合是衡量⼀个程序写的好坏的标准之⼀,
耦合是衡量模块与模块之间关联程度的指标 “⾼内聚,
低耦合”是⾯向对象编程的核⼼思想。
⼆、代理设计模式
当⼀个类的某些功能需要被别⼈来实现,但是既不明确是些什么 功能,⼜不明确谁来实现这些功能的时候,委托模式就可以派上⽤ 场。
⺫的是为了降低类之间的耦合性。
delegate也是⽤来解耦的,它不再简简单单让⺫标去执⾏⼀个动 作了
⽽是delegate去处理⼀些列事件、就像UITextFieldDelegate⼀ 样,能监测将要开始编辑,已经开始编辑、return按钮点击等等。
//告诉文件这是一个类
@class UIImageButton;
@protocol UIImageButtonDelegate <NSObject>
-(void)buttonImageViewClick:(UIImageButton *)imagebutton;
@end
@interface UIImageButton : UIImageView
@property(nonatomic,retain)id target;
@property(nonatomic,assign)SEL action;
//添加代理属性
//面试题 代理属性 为什么要声明成assign
//防止循环引用 从而造成的内存泄漏
/*
假如 A是B的代理 B也是A的代理 如果是retain
[[A alloc]init]; 计数1
[[B alloc]init]; 计数1
A.delegate = B; B:2
B.delegate = A; A:2
[A release]; 1
[B release]; 1
这时 A B 都释放不掉造成内存泄露
*/
@class UIImageButton;
@protocol UIImageButtonDelegate <NSObject>
-(void)buttonImageViewClick:(UIImageButton *)imagebutton;
@end
@interface UIImageButton : UIImageView
@property(nonatomic,retain)id target;
@property(nonatomic,assign)SEL action;
//添加代理属性
//面试题 代理属性 为什么要声明成assign
//防止循环引用 从而造成的内存泄漏
/*
假如 A是B的代理 B也是A的代理 如果是retain
[[A alloc]init]; 计数1
[[B alloc]init]; 计数1
A.delegate = B; B:2
B.delegate = A; A:2
[A release]; 1
[B release]; 1
这时 A B 都释放不掉造成内存泄露
*/
@property(nonatomic,assign)id<UIImageButtonDelegate> delegate;
-(void)dealloc
{
[_target release];
[super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action
{
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled = YES;
self.target = target;
self.action = action;
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.target performSelector:self.action withObject:nil];
if ([_delegate respondsToSelector:@selector(buttonImageViewClick:)]) {
[_delegate buttonImageViewClick:self];
}
//让代理去调用代理方法
// [_delegate buttonImageViewClick];
{
[_target release];
[super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action
{
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled = YES;
self.target = target;
self.action = action;
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.target performSelector:self.action withObject:nil];
if ([_delegate respondsToSelector:@selector(buttonImageViewClick:)]) {
[_delegate buttonImageViewClick:self];
}
//让代理去调用代理方法
// [_delegate buttonImageViewClick];
}
// 需求点击imageview 实现换背景颜色 并且遵循MVC模式
//实现协议中的方法
//
不管是
target/Action
设计模式还是代理模式还
mvc
模式中心只有一个解耦提高代码的复用性
三、UIImageView
UIImageView是iOS中⽤于显⽰图⽚的类,iOS中⼏乎所有看到的 图⽚,都是由这个类来显⽰的。
使⽤initWithImage:⽅法,创建UIImageView对象 使⽤initWithContentOfFile:⽅法,创建⼀个UIImage对象
四、⼿势识别器
UITapGestureRecognizer是轻拍⼿势识别器,能识别轻拍操作
UILongPressGestureRecognizer是⻓按⼿势识别器,能识别⻓按操作。
UIRotationGestureRecognizer是旋转⼿势识别器,能识别旋转操作。
UIPinchGestureRecognizer是捏合⼿势识别器,能识别捏合操作。
UIPanGestureRecognizer是平移⼿势识别器,能识别拖拽操作。
UISwipeGestureRecognizer是轻扫⼿势识别器,能识别拖拽操作。
UIScreenEdgePanGestureRecognizer是屏幕边缘轻扫识别器,是iOS7中新增的⼿势。
我们不会直接使⽤⼿势识别器这个抽象⽗类,⽽是根据需要使⽤特定的⼿势识别器创建对象。
1、创建UIxxxGestureRecognizer对象,使⽤initWithTarget:action:⽅法;
2、配置要识别的⼿势的相关信息;
3、将⼿势添加到某个视图上;
4、实现⼿势识别器⾥定义的⽅法
// 手势类 UIGestureRecongnizer
// 这个类是个抽象类 其具体功能 交给子类去实习
//创建imageview 添加手势
UIImageView *image = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
image.image = [UIImage imageNamed:@"5.jpg"];
[self.view addSubview:image];
image.userInteractionEnabled = YES;
[image release];
//轻拍
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//添加手势到视图上
[image addGestureRecognizer:tap];
[tap release];
//长按
//添加手势的方法
//1.初始化手势 添加手势触发调试的方法
//2.把手势添加到视图上
//3.释放手势
// UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];
// //设置长按时间
// longpress.minimumPressDuration = 2.0;
// //添加到视图上
// [image addGestureRecognizer:longpress];
// //释放
// [longpress release];
//旋转
// UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
// [image addGestureRecognizer:rotation];
// [rotation release];
//捏合
// UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchation:)];
// [image addGestureRecognizer:pinch];
// [pinch release];
//
// //平移手势
// UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
// [image addGestureRecognizer:pan];
// [pan release];
//
//轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
//设置左右扫方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[image addGestureRecognizer:swipe];
[swipe release];
UIScreenEdgePanGestureRecognizer *edgepan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgepan:)];
//设置从那个边缘扫
edgepan.edges = UIRectEdgeRight;
[image addGestureRecognizer:edgepan];
[edgepan release];
}
// 实现轻拍方法
-(void)tapAction:(UITapGestureRecognizer *)t
{
UIImageView *view = (UIImageView *)t.view;
view.image = [UIImage imageNamed:@"4.jpg"];
}
实现长按方法
//-(void)longAction:(UILongPressGestureRecognizer *)l
//{
// //判断一下状态 长按 只需触发一次
// if (l.state == UIGestureRecognizerStateBegan) {
// UIImageView *view = (UIImageView *)l.view;
// view.image = [UIImage imageNamed:@"3.jpg"];
// }
//}
//-(void)rotationAction:(UIRotationGestureRecognizer *)r
//{
// //形变属性 transform
// //参数一 要改变形变属性的视图
// //参数二 根据弧度去创建
// r.view.transform = CGAffineTransformRotate(r.view.transform,r.rotation);
// //每次转需要把旋转的角度 重置为0
// //因为要接替上一次的角度 开始旋转
// r.rotation = 0;
//}
//- (void)pinchation:(UIPinchGestureRecognizer *)p
//{
// //根据缩放的刻度(比例改变形变属性)
//
// p.view.transform = CGAffineTransformScale(p.view.transform, p.scale, p.scale);
// p.scale = 1;
// NSLog(@"123");
//}
// 实现平移方法的点(相对于要平移的视图)
//- (void)pan:(UIPanGestureRecognizer *)pan
//{
// CGPoint p = [pan translationInView:pan.view];
// // 根据这个点 改变形变属性
// pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
// //重置这个点
// [pan setTranslation:CGPointMake(0, 0) inView:pan.view];
//}
-(void)swipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"123");
}
-(void)edgepan:(UIScreenEdgePanGestureRecognizer *)edgepan
{
NSLog(@"321");
// 这个类是个抽象类 其具体功能 交给子类去实习
//创建imageview 添加手势
UIImageView *image = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
image.image = [UIImage imageNamed:@"5.jpg"];
[self.view addSubview:image];
image.userInteractionEnabled = YES;
[image release];
//轻拍
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//添加手势到视图上
[image addGestureRecognizer:tap];
[tap release];
//长按
//添加手势的方法
//1.初始化手势 添加手势触发调试的方法
//2.把手势添加到视图上
//3.释放手势
// UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];
// //设置长按时间
// longpress.minimumPressDuration = 2.0;
// //添加到视图上
// [image addGestureRecognizer:longpress];
// //释放
// [longpress release];
//旋转
// UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
// [image addGestureRecognizer:rotation];
// [rotation release];
//捏合
// UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchation:)];
// [image addGestureRecognizer:pinch];
// [pinch release];
//
// //平移手势
// UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
// [image addGestureRecognizer:pan];
// [pan release];
//
//轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
//设置左右扫方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[image addGestureRecognizer:swipe];
[swipe release];
UIScreenEdgePanGestureRecognizer *edgepan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgepan:)];
//设置从那个边缘扫
edgepan.edges = UIRectEdgeRight;
[image addGestureRecognizer:edgepan];
[edgepan release];
}
// 实现轻拍方法
-(void)tapAction:(UITapGestureRecognizer *)t
{
UIImageView *view = (UIImageView *)t.view;
view.image = [UIImage imageNamed:@"4.jpg"];
}
实现长按方法
//-(void)longAction:(UILongPressGestureRecognizer *)l
//{
// //判断一下状态 长按 只需触发一次
// if (l.state == UIGestureRecognizerStateBegan) {
// UIImageView *view = (UIImageView *)l.view;
// view.image = [UIImage imageNamed:@"3.jpg"];
// }
//}
//-(void)rotationAction:(UIRotationGestureRecognizer *)r
//{
// //形变属性 transform
// //参数一 要改变形变属性的视图
// //参数二 根据弧度去创建
// r.view.transform = CGAffineTransformRotate(r.view.transform,r.rotation);
// //每次转需要把旋转的角度 重置为0
// //因为要接替上一次的角度 开始旋转
// r.rotation = 0;
//}
//- (void)pinchation:(UIPinchGestureRecognizer *)p
//{
// //根据缩放的刻度(比例改变形变属性)
//
// p.view.transform = CGAffineTransformScale(p.view.transform, p.scale, p.scale);
// p.scale = 1;
// NSLog(@"123");
//}
// 实现平移方法的点(相对于要平移的视图)
//- (void)pan:(UIPanGestureRecognizer *)pan
//{
// CGPoint p = [pan translationInView:pan.view];
// // 根据这个点 改变形变属性
// pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
// //重置这个点
// [pan setTranslation:CGPointMake(0, 0) inView:pan.view];
//}
-(void)swipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"123");
}
-(void)edgepan:(UIScreenEdgePanGestureRecognizer *)edgepan
{
NSLog(@"321");
}
总结
target…action和delegate是很重要的设计模式,务必理解原理以 及熟练使⽤
⼿势识别器是很常⽤的类,在⽇常开发中经常使⽤,需要牢记每 个⼿势识别器的特点以及注意事项
transform是view的重要属性,在屏幕旋转⽅⾯⽤的⽐较多。