一、Touch手势

1、利用手势实现UIButton移动效果  实例代码

1) 创建一个继承自UIButton的类 MyButton.h  代码实现

 #import <UIKit/UIKit.h>
@interface MyButton : UIButton
@end

2)MyButton.m  的代码实现

 #import "MyButton.h"
@implementation MyButton
{
CGPoint _lastPoint;
} //手势开始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSLog(@"began:%@",NSStringFromCGPoint(point));
_lastPoint = point;
} -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGFloat offsetx = point.x - _lastPoint.x;
CGFloat offsety = point.y - _lastPoint.y;
self.center = CGPointMake(self.center.x + offsetx, self.center.y + offsety);
NSLog(@"moved:%@",NSStringFromCGPoint(point));
} -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSLog(@"end:%@",NSStringFromCGPoint(point));
}
@end

3)父视图中的代码实现

 #import "ViewController.h"
#import "MyButton.h"
@interface ViewController ()
{
MyButton *_v;
CGPoint _lastPoint;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
_v = [[MyButton alloc]initWithFrame:CGRectMake(, , , )];
_v.backgroundColor = [UIColor redColor];
[self.view addSubview:_v];
} //手势开始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
NSLog(@"began:%@",NSStringFromCGPoint(point));
_lastPoint = point;
} -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
CGFloat offsetx = point.x - _lastPoint.x;
CGFloat offsety = point.y - _lastPoint.y;
_v.center = CGPointMake(_v.center.x + offsetx, _v.center.y + offsety);
_lastPoint = point;
NSLog(@"moved:%@",NSStringFromCGPoint(point));
} -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
NSLog(@"end:%@",NSStringFromCGPoint(point));
} -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{ }
@end

2、利用Touch手势实现控件的缩放与旋转效果 实例代码

 #import "ViewController.h"
//遵守旋转与缩放的代理协议
@interface ViewController ()<UIGestureRecognizerDelegate>
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];
imgv.center = self.view.center;
[self.view addSubview:imgv];
imgv.image = [UIImage imageNamed:@""];
imgv.userInteractionEnabled = YES; //点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGes:)]; //设置该手势需要的手指数
tap.numberOfTouchesRequired = ; //设置该手势的点击次数
tap.numberOfTapsRequired = ;
[imgv addGestureRecognizer:tap]; //平移手势,拖拽手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
[imgv addGestureRecognizer:pan]; //缩放手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGes:)];
[imgv addGestureRecognizer:pinch];
pinch.delegate = self; //旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)];
[imgv addGestureRecognizer:rotation];
rotation.delegate = self;
} //返回值表示能否同时识别其他(相对于已经设置了代理的手势)手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
-(void)rotationGes:(UIRotationGestureRecognizer *)rotation
{
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
rotation.rotation = 0.0;
}
-(void)pinchGes:(UIPinchGestureRecognizer *)pinch
{
//transform:仿射变换
//pinch.scale,是缩放手势的捏合倍率
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale); //倍率还原
pinch.scale = 1.0;
}
-(void)panGes:(UIPanGestureRecognizer *)pan
{
//返回当前的手势的偏移量
CGPoint offset = [pan translationInView:pan.view];
//pan.view就是pan手势所加到的视图
pan.view.center = CGPointMake(pan.view.center.x + offset.x, pan.view.center.y + offset.y);
//移动以后,把偏移量归0
[pan setTranslation:CGPointZero inView:pan.view];
} -(void)tapGes:(UIGestureRecognizer *)tap
{
NSLog(@"==========");
}
@end
 
 
 

iOS阶段学习第35天笔记(Touch手势介绍)的更多相关文章

  1. iOS阶段学习第27天笔记(UIButton-UIImageView的介绍)

    iOS学习(UI)知识点整理 一.关于UIButton的介绍 1)概念:UIButton 是一种常用的控件,通过点击触发相应的功能 2)UIButton 的几种常用的状态        1.UICon ...

  2. iOS阶段学习第29天笔记(UITextField的介绍)

    iOS学习(UI)知识点整理 一.关于UITextField的介绍 1)概念: UITextField 是用于接收用户输入的一个控件 2)UITextField  初始化实例代码: //创建一个UIt ...

  3. iOS阶段学习第28天笔记(UIView的介绍)

    iOS学习(UI)知识点整理 一.关于UIVIew 的介绍 1)概念:UIView 是用于装载并展示各类控件的大容器,是iOS中所有UI控件的基类 2)UIView  初始化实例代码 UIView * ...

  4. iOS阶段学习第四天笔记(循环)

    iOS学习(C语言)知识点整理笔记 一.分支结构 1.分支结构分为单分支 即:if( ){ } ;多分支 即:if( ){ }else{ }  两种 2.单分支 if表达式成立则执行{ }里的语句:双 ...

  5. iOS 阶段学习第四天笔记(循环)

    iOS学习(C语言)知识点整理笔记 一.分支结构 1.分支结构分为单分支 即:if( ){ } ;多分支 即:if( ){ }else{ }  两种 2.单分支 if表达式成立则执行{ }里的语句:双 ...

  6. iOS 阶段学习第11天笔记(OC基础知识)

    iOS学习(OC语言)知识点整理 一.OC基础知识 1)#import  用于导入头文件,预处理阶段加载引用,只加载一次. 2)OC 依赖于Foundation框架下的头文件Foundation.h, ...

  7. iOS 阶段学习第七天笔记(函数、递归)

     iOS学习(C语言)知识点整理笔记 一.函数 1)概念:具有特定功能的代码块的封装 2)函数的定义: 函数类型+函数名(形参列表) 函数类型 函数名(形参类型1  形参名1,形参类型2   形参名2 ...

  8. iOS阶段学习第三天笔记(运算符)

    iOS学习(C语言)知识点整理笔记 1.运算符 一.算术运算符 1)表达式由变量.常量.运算符构成,有确定的类型和值 2)算术运算符包括: +(加),-(减),*(乘),/(除),%(模) 3)算术运 ...

  9. iOS 阶段学习第三天笔记(运算符)

    iOS学习(C语言)知识点整理笔记 1.运算符 一.算术运算符 1)表达式由变量.常量.运算符构成,有确定的类型和值 2)算术运算符包括: +(加),-(减),*(乘),/(除),%(模) 3)算术运 ...

随机推荐

  1. Hadoop学习笔记—15.HBase框架学习(基础实践篇)

    一.HBase的安装配置 1.1 伪分布模式安装 伪分布模式安装即在一台计算机上部署HBase的各个角色,HMaster.HRegionServer以及ZooKeeper都在一台计算机上来模拟. 首先 ...

  2. SQL Server null知多少?

    null是什么? 不知道.我是说,他的意思就是不知道(unknown). 它和true.false组成谓词的三个逻辑值,代表“未知”.与true和false相比,null最难以令人捉摸,因为它没有明确 ...

  3. 2013 duilib入门简明教程 -- 自绘标题栏(5)

        如果大家有做过标题栏的自绘,肯定会感慨各种不容易,并且现有的一些资料虽然完美的实现了功能,但是代码比较乱,需要自行整理.如果用duilib,就是小case啦.     duilib其实并没有区 ...

  4. Java 对象引用方式 —— 强引用、软引用、弱引用和虚引用

    Java中负责内存回收的是JVM.通过JVM回收内存,我们不需要像使用C语音开发那样操心内存的使用,但是正因为不用操心内存的时候,也会导致在内存回收方面存在不够灵活的问题.为了解决内存操作不灵活的问题 ...

  5. 对Big Table进行全表更新,导致 Replication 同步数据的过程十分缓慢

    在Publisher database中更新一个big table,数据行数是3.4亿多.由于没有更新 clustered Index key,因此,只产生了3.4亿多个Update Commands ...

  6. LINQ系列:Linq to Object投影操作符

    投影是指在将序列中的元素转换为一个自定义形式的操作.投影操作符Select和SelectMany用于选择出赋予了适当功能的值.SelectMany操作符可以处理多个集合. LINQ表达式语法: 1. ...

  7. OpenCASCADE View Manipulator

    OpenCASCADE View Manipulator eryar@163.com Abstract. When you finish modeling objects in the scene, ...

  8. JavaScript Prototype

    function Obj () { } Obj.a=0; Obj.fn=function(){ } console.log(Obj.a); console.log(typeof Obj.fn);//f ...

  9. Web APi入门之Self-Host寄宿及路由原理(二)

    前言 刚开始表面上感觉Web API内容似乎没什么,也就是返回JSON数据,事实上远非我所想,不去研究不知道,其中的水还是比较深,那又如何,一步一个脚印来学习都将迎刃而解. Self-Host 我们知 ...

  10. 5分钟用Spring4 搭建一个REST WebService

    前置技能 ① 使用maven来管理java项目 这个技能必须点一级,以便快速配置项目. 本文实际上是我学习Spring的过程中搬的官网上的demo,使用maven配置项目. ② jdk 1.8+   ...