一、target/ action 设计模式
耦合是衡量⼀一个程序写的好坏的标准之一,
耦合是衡量模块与模块之间关联程度的指标
“高内聚,低耦合”是⾯面向对象编程的核⼼心思想
@interface
TouchView :
UIView
{
id _target; // 记录对象被谁点了
SEL _action; // 点击完成对象要执行什么
{
id _target; // 记录对象被谁点了
SEL _action; // 点击完成对象要执行什么
}
@property
(
nonatomic
,
assign
)
SEL
action;
// 声明一个方法,模拟 Button 的添加点击事件
// 声明一个方法,模拟 Button 的添加点击事件
- (void)addTarget:(id)target action:(SEL)action;
@end
- (
void
)addTarget:(
id
)target action:(
SEL
)action;
{
// 进行赋值
_target = target;
_action = action;
{
// 进行赋值
_target = target;
_action = action;
// 给这个对象发送一个消息(给这个目标发送一个要执行的事件)withobject: 是可以携带的参数
}
//
初始化一个对象
//
处理事件
- ( void )touchesBegan:( NSSet *)touches withEvent:( UIEvent *)event
{
[ _target performSelector : _action withObject : self ]; // 可以是数组、字典、集合
NSLog ( @" 点我了 " );
- ( void )touchesBegan:( NSSet *)touches withEvent:( UIEvent *)event
{
[ _target performSelector : _action withObject : self ]; // 可以是数组、字典、集合
NSLog ( @" 点我了 " );
}
//
实现点击方法
- ( void )actionTouchView:( id *)touchView
{
TouchView *view = ( TouchView *)touchView;
view. backgroundColor = [ UIColor colorWithRed : arc4random ()% 256 / 255.0 green : arc4random ()% 256 / 255.0 blue : arc4random ()% 256 / 255.0 alpha : 1.000 ];
- ( void )actionTouchView:( id *)touchView
{
TouchView *view = ( TouchView *)touchView;
view. backgroundColor = [ UIColor colorWithRed : arc4random ()% 256 / 255.0 green : arc4random ()% 256 / 255.0 blue : arc4random ()% 256 / 255.0 alpha : 1.000 ];
}
二、代理设计模式
当一个类的某些功能需要被别人来实现,但是既不明确是些什么 功能,又不明确谁来实现这些功能的时候,委托模式就可以派上⽤场。目的是为了降低类之间的耦合性。
*
*
协议代理的步骤
1. 创建一个协议 ( 在协议里写方法 )
2. 声明一个代理属性
3. 遵守协议 实现方法的具体实现
4. 设置代理
5. 让代理执行协议里面的方法 ( 调用协议中的方法 )
6. 进行测试
1. 创建一个协议 ( 在协议里写方法 )
2. 声明一个代理属性
3. 遵守协议 实现方法的具体实现
4. 设置代理
5. 让代理执行协议里面的方法 ( 调用协议中的方法 )
6. 进行测试
- (
void
)touchesBegan:(
NSSet
*)touches withEvent:(
UIEvent
*)event
{
// 让代理去干活 ( 实现协议中的方法 )
[ _delegate changeSize : self ];
{
// 让代理去干活 ( 实现协议中的方法 )
[ _delegate changeSize : self ];
}
- (
void
)touchesMoved:(
NSSet
*)touches withEvent:(
UIEvent
*)event
{
// 随机改变颜色
[ _delegate changeColor : self ];
{
// 随机改变颜色
[ _delegate changeColor : self ];
}
#import
<UIKit/UIKit.h>
@class DelegateView ;
@class DelegateView ;
创建一个协议
@protocol
DelegateViewDelegate <
NSObject
>
- ( void )changeSize:( DelegateView *)delegateView;
- ( void )changeColor:( DelegateView *)delegateView;
@end
@interface DelegateView : UIView
// 声明一个属性设置一个代理
@property ( nonatomic , retain ) id < DelegateViewDelegate > delegate;
- ( void )changeSize:( DelegateView *)delegateView;
- ( void )changeColor:( DelegateView *)delegateView;
@end
@interface DelegateView : UIView
// 声明一个属性设置一个代理
@property ( nonatomic , retain ) id < DelegateViewDelegate > delegate;
@end
实现协议里面的方法
- (
void
)changeSize:(
DelegateView
*)delegateView
{
DelegateView *view = ( DelegateView *)[ self . view viewWithTag : 2 ];
NSLog ( @" 有了 " );
view. frame = CGRectMake ( 100 , 100 , arc4random () % 275 , arc4random () % 567 );
}
- ( void )changeColor:( DelegateView *)delegateView
{
DelegateView *view = ( DelegateView *)[ self . view viewWithTag : 1 ];
view. backgroundColor = [ UIColor colorWithRed : arc4random ()% 256 / 255.0 green : arc4random ()% 256 / 255.0 blue : arc4random ()% 256 / 255.0 alpha : 1.000 ];
{
DelegateView *view = ( DelegateView *)[ self . view viewWithTag : 2 ];
NSLog ( @" 有了 " );
view. frame = CGRectMake ( 100 , 100 , arc4random () % 275 , arc4random () % 567 );
}
- ( void )changeColor:( DelegateView *)delegateView
{
DelegateView *view = ( DelegateView *)[ self . view viewWithTag : 1 ];
view. backgroundColor = [ UIColor colorWithRed : arc4random ()% 256 / 255.0 green : arc4random ()% 256 / 255.0 blue : arc4random ()% 256 / 255.0 alpha : 1.000 ];
}
三、UIImageView
UIImageView是iOS中用于显示图⽚片的类,iOS
中几乎所有看到的 图片,都是由这个类来显示的。
initWithImage:
initWithContentOfFile:
四、手势识别器
手势识别器是对触摸事件做了封装,我们无需⾃己去判断某个手势
是否触发,手势识别器本身起到了识别作用,我们把重心放在识别之
后要做什么操作上面。
手势识别器是iOS中比较抽象的一个类,用于识别一个手势,所谓手势:有规律的触摸。
transform是view的⼀一个重要属性,它在矩阵层面上改变view的显示状态,能实现view
的缩放、旋转、平移等等功能。
- (
void
)viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view.
UIImageView *imageView = [[ UIImageView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];
imageView. image = [ UIImage imageNamed : @"faye" ];
[ self . view addSubview :imageView];
imageView. userInteractionEnabled = YES ;
[imageView release ];
// 手势识别器这个类 是一个抽象类
// 自己本身不实现什么具体功能
// 具体的功能都是由其子类来实现的
/**
// 轻拍
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(actionTap:)];
tap.numberOfTouchesRequired = 2; // 几根手指
tap.numberOfTapsRequired = 1; // 轻拍几次
[imageView addGestureRecognizer:tap]; // 手势添加到 view 上面
[tap release];
*/
/**
[ super viewDidLoad ];
// Do any additional setup after loading the view.
UIImageView *imageView = [[ UIImageView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];
imageView. image = [ UIImage imageNamed : @"faye" ];
[ self . view addSubview :imageView];
imageView. userInteractionEnabled = YES ;
[imageView release ];
// 手势识别器这个类 是一个抽象类
// 自己本身不实现什么具体功能
// 具体的功能都是由其子类来实现的
/**
// 轻拍
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(actionTap:)];
tap.numberOfTouchesRequired = 2; // 几根手指
tap.numberOfTapsRequired = 1; // 轻拍几次
[imageView addGestureRecognizer:tap]; // 手势添加到 view 上面
[tap release];
*/
/**
//
长按
*/
UILongPressGestureRecognizer *longPress = [[ UILongPressGestureRecognizer alloc ] initWithTarget : self action : @selector (actionLongPress:)];
longPress. minimumPressDuration = 1 ; // 大概最少长按多长时间 默认是 0.5 秒
[imageView addGestureRecognizer :longPress];
[longPress release ];
/**
*
// 旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(actionRotation:)];
[imageView addGestureRecognizer:rotation];
*/
/**
* 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(actionPinch:)];
[imageView addGestureRecognizer:pinch];
*/
/**
* 平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(actionPan:)];
[imageView addGestureRecognizer:pan];
*/
/**
* 清扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(actionSwipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:swipe];
[swipe release];
*/
/**
* 边缘扫 默认的 你不加 它也有
*/
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[ UIScreenEdgePanGestureRecognizer alloc ] initWithTarget : self action : @selector (actionScreenEdgePan:)];
screenEdgePan. edges = UIRectEdgeLeft ;
[imageView addGestureRecognizer :screenEdgePan];
[screenEdgePan release ];
}
// 抽象类里面有一个属性 是 UIView 类型的,把手势添加到哪个视图上面,当前的属性点的参数就是哪个
- ( void )actionScreenEdgePan:( UIScreenEdgePanGestureRecognizer *)screenEdgePan
{
// 只触发一次 刚一扫就触发
if (screenEdgePan. state == UIGestureRecognizerStateEnded ) {
NSLog ( @" 边缘扫 " );
UIImageView *view1 = ( UIImageView *)screenEdgePan. view ;
view1. image = [ UIImage imageNamed : @"faye1.jpg" ];
}
}
- ( void )actionSwipe:( UISwipeGestureRecognizer *)swipe
{
NSLog ( @" 左轻扫 " );
UIImageView *view1 = ( UIImageView *)swipe. view ;
view1. image = [ UIImage imageNamed : @"faye1.jpg" ];
}
- ( void )actionPan:( UIPanGestureRecognizer *)pan
{
NSLog ( @" 平移了 " );
NSLog ( @" x = %f, y = %f" , [pan translationInView :pan. view ]. x , [pan translationInView :pan. view ]. y );
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 )actionPinch:( UIPinchGestureRecognizer *)pinch
{
NSLog ( @" 捏合了 " );
// transform 形变属性 捏合
// 第二个参数 按照捏的刻度比例 形变
pinch. view . transform = CGAffineTransformScale (pinch. view . transform , pinch. scale , pinch. scale );
// 重置比例 为 1
pinch. scale = 1 ;
}
- ( void )actionRotation:( UIRotationGestureRecognizer *)rotation
{
NSLog ( @" 旋转了 " );
NSLog ( @"%f" , rotation. rotation );
// transform 形变属性
// 第一个属性 传入要创建哪个视图的形变属性
// 第二个属性 传入手势的弧度
rotation. view . transform = CGAffineTransformRotate (rotation. view . transform , rotation. rotation );
// 需要重置一下弧度
UILongPressGestureRecognizer *longPress = [[ UILongPressGestureRecognizer alloc ] initWithTarget : self action : @selector (actionLongPress:)];
longPress. minimumPressDuration = 1 ; // 大概最少长按多长时间 默认是 0.5 秒
[imageView addGestureRecognizer :longPress];
[longPress release ];
/**
*
// 旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(actionRotation:)];
[imageView addGestureRecognizer:rotation];
*/
/**
* 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(actionPinch:)];
[imageView addGestureRecognizer:pinch];
*/
/**
* 平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(actionPan:)];
[imageView addGestureRecognizer:pan];
*/
/**
* 清扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(actionSwipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:swipe];
[swipe release];
*/
/**
* 边缘扫 默认的 你不加 它也有
*/
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[ UIScreenEdgePanGestureRecognizer alloc ] initWithTarget : self action : @selector (actionScreenEdgePan:)];
screenEdgePan. edges = UIRectEdgeLeft ;
[imageView addGestureRecognizer :screenEdgePan];
[screenEdgePan release ];
}
// 抽象类里面有一个属性 是 UIView 类型的,把手势添加到哪个视图上面,当前的属性点的参数就是哪个
- ( void )actionScreenEdgePan:( UIScreenEdgePanGestureRecognizer *)screenEdgePan
{
// 只触发一次 刚一扫就触发
if (screenEdgePan. state == UIGestureRecognizerStateEnded ) {
NSLog ( @" 边缘扫 " );
UIImageView *view1 = ( UIImageView *)screenEdgePan. view ;
view1. image = [ UIImage imageNamed : @"faye1.jpg" ];
}
}
- ( void )actionSwipe:( UISwipeGestureRecognizer *)swipe
{
NSLog ( @" 左轻扫 " );
UIImageView *view1 = ( UIImageView *)swipe. view ;
view1. image = [ UIImage imageNamed : @"faye1.jpg" ];
}
- ( void )actionPan:( UIPanGestureRecognizer *)pan
{
NSLog ( @" 平移了 " );
NSLog ( @" x = %f, y = %f" , [pan translationInView :pan. view ]. x , [pan translationInView :pan. view ]. y );
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 )actionPinch:( UIPinchGestureRecognizer *)pinch
{
NSLog ( @" 捏合了 " );
// transform 形变属性 捏合
// 第二个参数 按照捏的刻度比例 形变
pinch. view . transform = CGAffineTransformScale (pinch. view . transform , pinch. scale , pinch. scale );
// 重置比例 为 1
pinch. scale = 1 ;
}
- ( void )actionRotation:( UIRotationGestureRecognizer *)rotation
{
NSLog ( @" 旋转了 " );
NSLog ( @"%f" , rotation. rotation );
// transform 形变属性
// 第一个属性 传入要创建哪个视图的形变属性
// 第二个属性 传入手势的弧度
rotation. view . transform = CGAffineTransformRotate (rotation. view . transform , rotation. rotation );
// 需要重置一下弧度
rotation.rotation = 0;
}
- (
void
)actionLongPress:(
UILongPressGestureRecognizer
*)longPress
{
if (longPress. state == UIGestureRecognizerStateBegan ) {
NSLog ( @" 按按按 " ); // 长按换张图片
UIImageView *view1 = ( UIImageView *)longPress. view ;
view1. image = [ UIImage imageNamed : @"faye1.jpg" ];
}
}
{
if (longPress. state == UIGestureRecognizerStateBegan ) {
NSLog ( @" 按按按 " ); // 长按换张图片
UIImageView *view1 = ( UIImageView *)longPress. view ;
view1. image = [ UIImage imageNamed : @"faye1.jpg" ];
}
}
- (
void
)actionTap:(
UITapGestureRecognizer
*)tap
{
NSLog ( @" 拍拍拍 " );
{
NSLog ( @" 拍拍拍 " );
}