iOS阶段学习第35天笔记(Touch手势介绍)
一、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手势介绍)的更多相关文章
- iOS阶段学习第27天笔记(UIButton-UIImageView的介绍)
iOS学习(UI)知识点整理 一.关于UIButton的介绍 1)概念:UIButton 是一种常用的控件,通过点击触发相应的功能 2)UIButton 的几种常用的状态 1.UICon ...
- iOS阶段学习第29天笔记(UITextField的介绍)
iOS学习(UI)知识点整理 一.关于UITextField的介绍 1)概念: UITextField 是用于接收用户输入的一个控件 2)UITextField 初始化实例代码: //创建一个UIt ...
- iOS阶段学习第28天笔记(UIView的介绍)
iOS学习(UI)知识点整理 一.关于UIVIew 的介绍 1)概念:UIView 是用于装载并展示各类控件的大容器,是iOS中所有UI控件的基类 2)UIView 初始化实例代码 UIView * ...
- iOS阶段学习第四天笔记(循环)
iOS学习(C语言)知识点整理笔记 一.分支结构 1.分支结构分为单分支 即:if( ){ } ;多分支 即:if( ){ }else{ } 两种 2.单分支 if表达式成立则执行{ }里的语句:双 ...
- iOS 阶段学习第四天笔记(循环)
iOS学习(C语言)知识点整理笔记 一.分支结构 1.分支结构分为单分支 即:if( ){ } ;多分支 即:if( ){ }else{ } 两种 2.单分支 if表达式成立则执行{ }里的语句:双 ...
- iOS 阶段学习第11天笔记(OC基础知识)
iOS学习(OC语言)知识点整理 一.OC基础知识 1)#import 用于导入头文件,预处理阶段加载引用,只加载一次. 2)OC 依赖于Foundation框架下的头文件Foundation.h, ...
- iOS 阶段学习第七天笔记(函数、递归)
iOS学习(C语言)知识点整理笔记 一.函数 1)概念:具有特定功能的代码块的封装 2)函数的定义: 函数类型+函数名(形参列表) 函数类型 函数名(形参类型1 形参名1,形参类型2 形参名2 ...
- iOS阶段学习第三天笔记(运算符)
iOS学习(C语言)知识点整理笔记 1.运算符 一.算术运算符 1)表达式由变量.常量.运算符构成,有确定的类型和值 2)算术运算符包括: +(加),-(减),*(乘),/(除),%(模) 3)算术运 ...
- iOS 阶段学习第三天笔记(运算符)
iOS学习(C语言)知识点整理笔记 1.运算符 一.算术运算符 1)表达式由变量.常量.运算符构成,有确定的类型和值 2)算术运算符包括: +(加),-(减),*(乘),/(除),%(模) 3)算术运 ...
随机推荐
- CSS选择器小结
在CSS3中是提倡使用选择器来将样式与元素直接绑定在一起的. 网页开发过程中,我们需要定义很多class来应用到不同的元素上,由于class本身是没有语义而且是可以复用的,所以过度使用class会使得 ...
- [每日电路图] 10、两种MOS管的典型开关电路
下图是两种MOS管的典型应用:其中第一种NMOS管为高电平导通,低电平截断,Drain端接后面电路的接地端:第二种为PMOS管典型开关电路,为高电平断开,低电平导通,Drain端接后面电路的VCC端. ...
- 在VMWare中建立Hadoop虚拟集群的详细步骤(使用CentOS)
最近在学习Hadoop,于是想使用VMWare建立一个虚拟的集群环境.网上有很多参考资料,但参照其步骤进行设置时却还是遇到了不少问题,所以在这里详细写一下我的配置过程,以及其中遇到的问题及相应的解决方 ...
- jws.mono脚本安装详解
就在最近两天,最新版本的jws.mono上线了,这个版本除了提供与之前版本拥有的功能外,还额外提供了一个“自动化”的安装脚本,通过执行该脚本,jws.mono将自动快速的安装到指定的目录,同时,通过改 ...
- Node.js学习笔记
相关介绍 1.Node.js或者Node,是一个可以让javascript运行在服务器端的平台. 2.Node.js是一个为实时Web应用开发而诞生的语言,它从诞生之初就充分考虑了再实时响应.超大规模 ...
- CWR Mobile简介
原创地址:http://www.cnblogs.com/jfzhu/p/4266671.html 转载请注明出处 (一)CWR公司背景 Dynamics CRM除了自己Out of Box对移动设备的 ...
- HTTPS那些事(二)SSL证书(转载)
原创地址:http://www.guokr.com/post/116169/ 从第一部分HTTP工作原理中,我们可以了解到HTTPS核心的一个部分是数据传输之前的握手,握手过程中确定了数据加密的密 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (44) ------ 第八章 POCO之POCO中使用值对象和对象变更通知
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 8-4 POCO中使用值对象(Complex Type--也叫复合类型)属性 问题 ...
- jquery做一些小的特效
在文本框里输入内容到添加到下拉列表里,移除下拉里的内容 1 <!DOCTYPE html> <html> <head> <meta charset=" ...
- php后台增加删除修改跳转页面
第一步 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...