UI-target...action设计模式,手势识别器.UIimageview target-action设计模式 UIImageView

   

  iOS设计模式之Target-Action主要是为了降低代码的耦合性。顾名思义      Target-Action模式就是指  目标-动作模式,它贯穿于iOS开发始终。

提到Target-Action,先说2个词     “高内聚,低耦合”      这主要是评价一个软件的好坏

它评判软件的好坏主要靠模板之间内聚是否高,模块间耦合度是否低。

    其实Target-action模式很简单,就是当某个事件发生时,调用那个对象中的那个方法。比如:点击按钮时,调用Controller里边的 click方法。“那个对象”就是Target,“那个方法”就是Action,及Controller是Targer,click方法是action。

一般Target都是Controller,而Action有它自己固有的格式:-(IBAction)click:(id)sender。

   对于Target-Action模式更加专业点讲,可以这么说:Target-Action是一种当一个事件发生时候,一个对象携带发送一个消息到另一个 对象的必要的信息设计模式。存储的信息包括两类数据:标识所被调用的方法的动作选择器,和一个接收消息的目标。当被称作动作消息的事件发生的时候消息开始 发送。尽管target可以是任何对象,甚至是框架对象,典型代表是以一种应用的特殊方式处理action message的一个自定义控制器。引 发一个动作消息的事件可以是任何事物,比如对象发送消息可以为任何对象一样。举个例子:手势识别对象可能会发送一个动作消息给另一个对象当手势被识别的时 候。然而target-action范例最普遍的发现在控制器例如按钮或者滑动条。当一个用户操作一个控制对象,它发送消息给特殊的对象。控制对象是 UIControl的子类。action selecter和target object都是控制对象的属性。

     Target-Action模式主要是在MVC模式中 V--C 之间进行通信的。V(view:视图)只负责发送对应的action给target,并不关心target具体做什么。这样代码的耦合性就松开了。

target/action设计模式

类为委托方
控制器为代理方
方法为协议

//.h文件
#import <UIKit/UIKit.h>

@interface TapView : UIView

@property (nonatomic,assign) id target;
@property (nonatomic,assign) SEL action;

@end
//视图的.m文件
#import "TapView.h"
@implementation TapView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //这里负责调用
    //外界传什么方法,我就执行什么方法,不需要知道方法到底是怎么实现的.
    //如果外界的方法有参数,那么这个参数一定是TapView*类型的
    [_target performSelector:self.action withObject:self];
}
//根视图控制器控制
//控制器的.m文件
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    TapView *tap1 = [[TapView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    tap1.backgroundColor = [UIColor redColor];
    [self.view addSubview:tap1];
    tap1.tag = 1001;
    tap1.target = self;     //target=控制器
    tap1.action = @selector(randomColor:);//选择要触发的事件
    [tap1 release];
    }

代理设计模式

控制有一些是时间点,控制器可以实现这个代理方法,以便在适应的时候做适应的事
对于一些时间点,想让我的代理对象做什么事

//代理设计模式要先写一个协议
//协议名称以类名开头加上Delegate .h文件
@protocol TapViewDelegate <NSObject>
@optional
//当视图刚开始被触摸的时候,代理方要去实现的方法.
- (void)tapViewBegan:(TapView *)tapView;
//当视图在拖动过程中,想要代理方要去实现方式
- (void)tapViewMove:(TapView *)tapView;
//当视图结束触摸的时候,代理方要去实现的方法
- (void)tapViewEnd:(TapView *)tapView;
@end
@interface TapView : UIView
@property (nonatomic,assign) id<TapViewDelegate> detegate; //设计接收协议的代理对象
@end
//.m文件 如果代理不为空,切响应了某个事件 就触发 代理一般用在时间点上
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (_detegate != nil && [_detegate respondsToSelector:@selector(tapViewBegan:)])
    {
         [_detegate tapViewBegan:self];
    }
   
}

UIImageView

//写法1:创建一个图片视图
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2753441426829430"]];
    imageView.frame = self.view.bounds ;
    [self.view addSubview:imageView];
//写法2:创建一个gif视图    
//如何让UIImageView播放多张图片,GIF动态图
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 150, 350, 350)];
    NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:4];
    for (int i = 1; i <= 4; i++)
    {
        NSString *imageName = [NSString stringWithFormat:@"w12%d.tiff",i];
        UIImage *image = [UIImage imageNamed:imageName];
        [imageArray addObject:image];
    }
    
    imageView.animationImages = imageArray;//指定需要做动画的图片
    imageView.animationDuration = 0.5;//设置播放的时间 速率
    imageView.animationRepeatCount = 0; //设置循环次数
    [self.view addSubview:imageView];
    [imageView startAnimating];//让其开始播放
    [imageView release];   
    //写法3:通过图片路径加载图片
        //通过文件路径来加载一个图片
    UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"/Users/anhao/Desktop/2753441426829430.png"]];
    imageView1.frame = self.view.bounds ;
    [self.view addSubview:imageView1];
 

手势识别器:

手势识别器:是对触摸事件做了封装,无需自己去判断某个手势是否触发,手势识别器本身起到了识别作用,所在我们可以把重心放在识别之后该去做什么操作上面.很方便.

手势识别器是iOS中比较抽象的一个类,用于识别一个手势,所谓的手势:有规律的触摸.

手势识别器有7个子类:

分别是:轻怕手势,轻移手势,清扫手势,缩放手势,旋转手势,长按手势,以及屏幕边缘平移手势.

一旦指定的手势别识别了,就可以执行自定义好的操作了.

UITapGestureRecognizer是轻拍⼿手势识别器,能识别轻拍操作

UILongPressGestureRecognizer是⻓长按⼿手势识别器,能识别⻓长按操作。

UIRotationGestureRecognizer是旋转⼿手势识别器,能识别旋转操作。

UIPinchGestureRecognizer是捏合⼿手势识别器,能识别捏合操作。

UIPanGestureRecognizer是平移⼿手势识别器,能识别拖拽操作。

UISwipeGestureRecognizer是轻扫⼿手势识别器,能识别拖拽操作。

UIScreenEdgePanGestureRecognizer是屏幕边缘轻扫识别器,是iOS7中新增的⼿手势。 

如何使用识别器:

我们不会直接使用手势识别器这个抽象父类,而是根据需要使用特定的手势识别器创建对象.

1.创建UIXXXGestureRecognizer对象,使用initWithTarget:action:方法

2.配置要识别的手势的相关信息

3.将手势添加到某个视图上

4.实现手势识别器里定义的方法

view 的transform 属性

transform 是 view的一个重要属性.它在矩阵层面上改变view的显示状态.能实现view的缩放.旋转.平移等等功能.

手势识别器的使用

1.轻击手势(TapGestureRecognizer)的添加

      初始化代码TapGestureRecongnizer的代码如下:

UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView
1     //新建tap手势
2     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
3     //设置点击次数和点击手指数
4     tapGesture.numberOfTapsRequired = 1; //点击次数
5     tapGesture.numberOfTouchesRequired = 1; //点击手指数
6     [self.view addGestureRecognizer:tapGesture];
UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView

    在回调方法中添加相应的业务逻辑:

1 //轻击手势触发方法
2 -(void)tapGesture:(id)sender
3 {
4     //轻击后要做的事情        
5 }

    2.长按手势(LongPressGestureRecognizer)

      初始化代码:

UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView
1     //添加长摁手势
2     UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
3     //设置长按时间
4     longPressGesture.minimumPressDuration = 0.5; //(2秒)
5     [self.view addGestureRecognizer:longPressGesture];
UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView

     在对应的回调方法中添加相应的方法(当手势开始时执行):

UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView
 1 //常摁手势触发方法
 2 -(void)longPressGesture:(id)sender
 3 {
 4     UILongPressGestureRecognizer *longPress = sender;
 5     if (longPress.state == UIGestureRecognizerStateBegan)
 6     {
 7         UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"长按触发" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
 8         [alter show];
 9     }
10 }
UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView

    代码说明:手势的常用状态如下

      开始:UIGestureRecognizerStateBegan

      改变:UIGestureRecognizerStateChanged

      结束:UIGestureRecognizerStateEnded

        取消:UIGestureRecognizerStateCancelled

      失败:UIGestureRecognizerStateFailed

    3.轻扫手势(SwipeGestureRecognizer)

      在初始化轻扫手势的时候得指定轻扫的方向,上下左右。 如果要要添加多个轻扫方向,就得添加多个轻扫手势,不过回调的是同一个方法。

      添加轻扫手势,一个向左一个向右,代码如下:

UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView
 1     //添加轻扫手势
 2     UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
 3     //设置轻扫的方向
 4     swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //默认向右
 5     [self.view addGestureRecognizer:swipeGesture];
 6     
 7     //添加轻扫手势
 8     UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
 9     //设置轻扫的方向
10     swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //默认向右
11     [self.view addGestureRecognizer:swipeGestureLeft];
UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView

     回调方法如下:

UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView
 1 //轻扫手势触发方法
 2 -(void)swipeGesture:(id)sender
 3 {
 4     UISwipeGestureRecognizer *swipe = sender;
 5     if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
 6     {
 7         //向左轻扫做的事情
 8     }
 9     if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
10     {
11         //向右轻扫做的事情
12     }
13 }
14     
UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView

    4.捏合手势(PinchGestureRecognizer)

      捏合手势初始化

1     //添加捏合手势
2     UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
3     [self.view addGestureRecognizer:pinchGesture];

      捏合手势要触发的方法(放大或者缩小图片):

UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView
 1 ////捏合手势触发方法
 2 -(void) pinchGesture:(id)sender
 3 {
 4      UIPinchGestureRecognizer *gesture = sender;
 5     
 6     //手势改变时
 7     if (gesture.state == UIGestureRecognizerStateChanged)
 8     {
 9         //捏合手势中scale属性记录的缩放比例
10         _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
11     }
12     
13     //结束后恢复
14     if(gesture.state==UIGestureRecognizerStateEnded)
15     {
16         [UIView animateWithDuration:0.5 animations:^{
17             _imageView.transform = CGAffineTransformIdentity;//取消一切形变
18         }];
19     }
20 }
UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView

    5.拖动手势(PanGestureRecognizer)

      拖动手势的初始化


UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)]; [redView addGestureRecognizer:panGesture];

    拖动手势要做的方法(通过translationInView获取移动的点,和TouchesMoved方法类似)

UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView
1 //拖动手势
UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView

- (void)panGesture:(UIPanGestureRecognizer *)panGesture
{
    CGPoint point = [panGesture translationInView:panGesture.view];

    panGesture.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
    
    panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);
    

将之前增量清零
    
    [panGesture setTranslation:point inView:panGesture.view];
    
   

获取的是手指位置的改变量  , 存储在x轴以及y轴的改变量

 CGPoint point = [panGesture translationInView:panGesture.view];
    

 panGesture.view.center = CGPointMake(panGesture.view.center.x+ point.x,panGesture.view.center.y+ point.y);

将之前的增量清零 重置之前的增量

   [panGesture setTranslation:CGPointZero inView:panGesture.view];

 panGesture.view.backgroundColor  = [UIColor random];
}

  

    6.旋转手势(RotationGestureRecognizer)

      旋转手势的初始化

1     //添加旋转手势
2     UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
3     [self.view addGestureRecognizer:rotationGesture];

      旋转手势调用的方法:

UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView
 1 //旋转手势
 2 -(void)rotationGesture:(id)sender
 3 {
 4     
 5     UIRotationGestureRecognizer *gesture = sender;
 6     
 7     if (gesture.state==UIGestureRecognizerStateChanged)
 8     {
 9         _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
10     }
11     
12     if(gesture.state==UIGestureRecognizerStateEnded)
13     {
14         
15         [UIView animateWithDuration:1 animations:^{
16             _imageView.transform=CGAffineTransformIdentity;//取消形变
17         }];
18     }
19     
20 }
UI-target...action设计模式,手势识别器.UIimageview
target-action设计模式
UIImageView

  

  上面的东西没有多高深的技术,就是对iOS开发中的手势做了一下小小的总结,温故一下基础知识。在之前的博客中也有用到手势识别的内容,就是没有系统的梳理一下手势识别的知识,本篇博客做一个基础的补充吧。欢迎批评指正,转载请注明出处。