<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

本文涉及的产品
转发路由器TR,750小时连接 100GB跨地域
简介: DesignModeler : 设计模式    GestureRecginzer:手势识别作者:韩俊强原创版权地址:http://blog.
DesignModeler : 设计模式     GestureRecginzer:手势识别
作者:韩俊强
1、第一种设计模式
DelegateViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    //改变自身颜色
   
ActionView *redView = [[ActionView alloc]initWithFrame:CGRectMake(20, 20, 280, 100)];
    redView.
tag = 101;
   [ redView
addTarget:self action:@selector(changeselfBackground :)];
    redView.
backgroundColor = [UIColor randomClolor];
    [
self.view addSubview:redView];
    [redView release];}
#pragma mark --Target....action 设计模式的方法实现
//改变自身颜色
- (void)changeselfBackground : (ActionView *)view{
    view.backgroundColor = [UIColor randomClolor]; 
}
ActionView.h 
@interface ActionView : UIView
//给外界提供一个接口(方法),用来给ActionView 制定事件的响应对象(目标 target),以及target目标响应的方法
- (
void)addTarget : (id)target action:(SEL)action;
@end
ActionView.m
@interface ActionView ()
{
   
id _target;//存储传入的响应对象
   
SEL _action;//存储响应对象执行的方法

}
@end
@implementation ActionView
 ActionView 是自定义的视图,我们想把它封装成完成的类,一个完整的类,就是无论你再有什么样的需求,都无需去修改它的源文件
 以前的处理方式不够灵活,因为ActionView创建的对象没接到触摸消息,都要自己去处理事件,所以每创建一个对象,提出一个新的需求都要区修改它的源文件,此时ActionView对象和事件就捆绑到一起了,此时耦合性太强
 通过target ...action设计模式,将事件交由其他对象处理,让我们的ActionView像UIButton一样灵活,此时它只需负责通过目标干活就可以了,此时的ActionView对象和事件就不再捆绑到一起了,耦合性降低了,内聚就升高了
- (void)addTarget : (id)target action:(SEL)action{
   
//此时在这个方法中要把外界传进来的目标对象,和目标对象要执行的方法存储起来
   
_target = target;
   
_action = action;
}
- (
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//开始触摸,当ActionView 对象接受到触摸事件的时候,自己不处理事件,需要由_target 来处理
 //目标选择方法去为self执行事件,而且方法中如果有参数,参数就指的是addTarget ...action 方法的调用
    [_target performSelector:_action withObject:self];
DesignModeler <wbr>GestureRecginzer <wbr>UI_05

}===================================================
2、第二种模式  使用代理和设计模式,完成touchView的触摸事件的响应操作,其实就是通过这种方式解除 事件和touchView的耦合
 使用代理和协议步骤:
 1.制订协议(代理要完成的任务)
 2.定义代理属性(存储代理对象)
 3.在其他文件中指定touchView的代理对象 (帮touchView干活)
 4.让代理对象服从协议(代理对象答应帮touchView)干活
 5.让代理对象实现协议中的方法(代理对象知道怎么去干活)
 6.委托人通知代理对象什么时候执行协议中的方法(通知代理去干活)
TouchView.h
//第一步:制定协议
@class TouchView;
@protocol TouchViewDelegate <</span>NSObject>
@optional
//刚开始触摸的时候让代理对象完成这个方法
- (void)touchBeganWithTouchView : (TouchView *)touchView;
@end
@interface TouchView : UIView
//第二步:第一代理属性
@end
————————————————————————————
TouchView.m

@implementation TouchView
- (
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
   
//第六步:让代理对象干活
   
if ([_delegate respondsToSelector:@selector(touchBeganWithTouchView:)]) {
        [_delegate touchBeganWithTouchView:self];}}
 ————————————————————————————  
DelegateViewController.m  
//第四步:让代理对象遵循协议
@interface DelegateViewController ()<</span>TouchViewDelegate>
@end
@implementation DelegateViewController
- (
void)viewDidLoad {
    [
super viewDidLoad];
   
self.view.backgroundColor = [UIColor yellowColor];
   
TouchView *redView = [[TouchView alloc]initWithFrame:CGRectMake(20, 30, 280, 100)];
    redView.
backgroundColor = [UIColor redColor];
   
//第三步:指定代理对象
    redView.
delegate = self;
    [
self.view addSubview:redView];
    [redView
release];
}
//第五步:实现协议中的方法
- (void)touchBeganWithTouchView:(TouchView *)touchView{
    touchView.backgroundColor = [UIColor randomClolor];//随机颜色自己做一下封装
} DesignModeler <wbr>GestureRecginzer <wbr>UI_05

======================================================================
3、GestureRecginzer:手势识别
AppDelegate.m
GestureViewController *gesture = [[ GestureViewController alloc ] init ];
    self . window . rootViewController = gesture;
    [gesture release];
——————————————————————
封装颜色随机值文件下载 http://pan.baidu.com/s/1gdm6JEb
GestureViewController.m
#import "GestureViewController.h"
#import "UIColor+RandomColor.h"
- ( void )viewDidLoad {
    [super viewDidLoad];
    self . view . backgroundColor = [ UIColor colorWithPatternImage :[ UIImage imageNamed : @"444.jpg" ]];
  // UIGestureRecognizer 手势识别器的基类 ,拓为我们提供了手势识别器的一些基本的功能,我们在屏幕上的手势全部由手势识别器来完成识别,此时我们只需要关心手势识别之后应该做出什么处理 ,它由6大子类,还有一个孙子类(屏幕边缘手势,是平移手势的子类)
  
 
UIView *redView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 20 , 320 , 500 )];
    redView.
backgroundColor = [ UIColor redColor ];
    [
self . view addSubview :redView];
    [redView release];
————————————————————————————
1、轻拍手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap :)];
    //配置属性
    //设置轻拍手势触发时间所需的轻拍次数
    tapGesture.numberOfTapsRequired = 1;//默认为1下
    //设置轻拍需要的手指对象个数
    tapGesture.numberOfTouchesRequired = 2;
    //给redView添加轻拍的手势对象
    [redView addGestureRecognizer:tapGesture];
    [tapGesture release];
DesignModeler <wbr>GestureRecginzer <wbr>UI_05

#pragma mark 轻拍手势的方法实现
- (void)handleTap : (UITapGestureRecognizer *)tapGesture{
     tapGesture.view.backgroundColor = [UIColor randomClolor];}
————————————————————————————————————————
2、长按手势 UILongPressGestureRecognizer  
   UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress :)];
    //设置长按手势最小多长时间触发方法
    longGesture.minimumPressDuration = 1;//默认为.05秒
   //在视图上添加手势对象
    [redView addGestureRecognizer:longGesture];
    //释放
    [longGesture release];
DesignModeler <wbr>GestureRecginzer <wbr>UI_05

#pragma mark   长按手势方法实现
- (void) handleLongPress : (UILongPressGestureRecognizer *)longPress{
   
//改变手势所在视图的父视图的颜色
   
//根据手势状态,选择执行相应操作
   
if (longPress.state == UIGestureRecognizerStateBegan) {
            longPress.
view.superview.backgroundColor = [UIColor randomClolor];
    }
}
————————————————————————
3、轻扫手势 UISwipeGestureRecognizer   
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
  
//设置轻扫的方向
    swipeGesture.
direction UISwipeGestureRecognizerDirectionRight;
    [redView
addGestureRecognizer:swipeGesture];
    [swipeGesture release];
DesignModeler <wbr>GestureRecginzer <wbr>UI_05

#pragma mark  轻扫手势方法的实现
- (void)handleSwipe: (UISwipeGestureRecognizer *)swipeGesture{
    swipeGesture.
view.backgroundColor = [UIColor randomClolor];
}
————————————————————————-
4、平移手势UIPanGestureRecognizer   
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan : )];
    [redView
addGestureRecognizer:panGesture];
    [panGesture release];
DesignModeler <wbr>GestureRecginzer <wbr>UI_05

 #pragma mark  平移手势方法的实现
- (void)handlePan : (UIPanGestureRecognizer *)panGesture{
   
//1.获取平移增量
   
CGPoint point = [panGesture translationInView:panGesture.view];
   
//2.仿射变换(变形)
   
//第一个参数:变形之前的视图位置和大小
   
//第二个参数:x轴上的形变量(x轴上的增量)
   
//第三个参数:y轴上的形变量(y轴上的增量)

    panGesture.
view.transform = CGAffineTransformTranslate(panGesture.view.transform,point.x, point.y);
   
//3.将之前的增量清0;
   
//CGPointZero  代表{0,0}点     CGPointMake(0,0);
    [panGesture setTranslation:CGPointZero inView:panGesture.view];
————————————————————————————————
5、捏合手势   UIPinchGestureRecognizer  
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch :)];
    [redView addGestureRecognizer:pinchGesture];
    [pinchGesture release];
}
DesignModeler <wbr>GestureRecginzer <wbr>UI_05

 #pragma mark  捏合手势方法的实现
- (void)handlePinch : (UIPinchGestureRecognizer *)pinchGesture{
    pinchGesture.
view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);
   
//scale 缩放比例
   
//将之前的形变量置为1
   pinchGesture.
scale = 1;
}
————————————————————————————————
6.旋转手势  UIRotationGestureRecognizer 
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotationGesture : )];
    [redView addGestureRecognizer:rotationGesture];
    [rotationGesture release];
DesignModeler <wbr>GestureRecginzer <wbr>UI_05

 #pragma mark  旋转手势方法的实现
- (void)handleRotationGesture : (UIRotationGestureRecognizer *)RotationResture{
    RotationResture.
view.transform = CGAffineTransformRotate(RotationResture.view.transform, RotationResture.rotation);
   
//将之前的角度增量置为0
    RotationResture.
rotation = 0;
}
——————————————————————————————
7.屏幕边缘手势UIScreenEdgePanGestureRecognizer 
    //屏幕边缘的手势必须和硬件设备的边缘重合,此时这个手势才有作用
   
UIScreenEdgePanGestureRecognizer *screenGesture = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(handleScreenGesture: )];
   //设置屏幕的边缘
    screenGesture.edges = UIRectEdgeLeft;
    [redView
addGestureRecognizer:screenGesture];
    [screenGesture release];
DesignModeler <wbr>GestureRecginzer <wbr>UI_05
 #pragma mark  屏幕边缘手势方法的实现
- (void) handleScreenGesture : (UIScreenEdgePanGestureRecognizer *)screen{
   
NSLog(@"小样你能行吗?");
    }
=================================================
欢迎学习本文,未经博主允许,禁止转载!
目录
相关文章
|
Web App开发 数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
可伸缩系统的架构经验 Feb 27th, 2013 | Comments 最近,阅读了Will Larson的文章Introduction to Architecting System for Scale,感觉很有价值。
2430 0
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
一个典型的星型模式包括一个大型的事实表和一组逻辑上围绕这个事实表的维度表。  事实表是星型模型的核心,事实表由主键和度量数据两部分组成。
605 0
|
新零售 Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
各大互联网公司架构演进之路汇总 大型网站架构演化历程 大型网站架构技术一览 Web 支付宝和蚂蚁花呗的技术架构及实践 支付宝的高可用与容灾架构演进 聚划算架构演进和系统优化 (视频+PPT) 淘宝交易系统演进之路 (专访) 淘宝数据魔方技术架构解析 淘宝技术发展历程和架构经验分享(视频+PPT)(2.
2181 0
|
机器学习/深度学习 Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
1.样本要随机化,防止大数据淹没小数据 2.样本要做归一化。关于归一化的好处请参考:为何需要归一化处理3.激活函数要视样本输入选择(多层神经网络一般使用relu)4.
760 0
|
Web App开发 前端开发
|
Web App开发 前端开发 数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
最近在使用Spark Streaming过程中,对foreachRDD有点疑问,查阅资料后记录如下: foreachRDD(func)的官方解释为 The most generic output operator ...
865 0
|
Web App开发 前端开发 Linux
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
一般来说,Linux的虚拟内存会根据系统负载自动调整。内存页(page)swap到磁盘会显著的影响Kafka的性能,并且Kafka重度使用page cache,如果VM系统swap到磁盘,那说明没有足够的内存来分配page cache。
740 0
|
Web App开发 前端开发 Java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
Jboss7中ejb3使用@Schedule调度器总是每分钟执行 1 问题:当我尝试着去开发一个ejb3的@Schedule调度器来执行我预定每5秒钟执行一次的任务时。
821 0
|
Web App开发 测试技术 应用服务中间件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
一 概要说明 使用nginx搭建流媒体直播平台,目的就是要支持rtmp协议,实现用户使用rtmp(rtmp://192.168.201.128/myapp)协议推送流到服务器。
1754 0

热门文章

最新文章