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)算术运 ...
随机推荐
- ABP框架理论学习之后台工作(Jobs)和后台工作者(Workers)
返回总目录 本篇目录 介绍 后台工作 后台工作者 让你的应用程序一直运行 介绍 ABP提供了后台工作和后台工作者,它们会在应用程序的后台线程中执行一些任务. 后台工作 后台工作以队列和持续的方式在后台 ...
- 【译】PHP的变量实现(给PHP开发者的PHP源码-第三部分)
文章来自:http://www.aintnot.com/2016/02/12/phps-source-code-for-php-developers-part3-variables-ch 原文:htt ...
- XCode的个人使用经验
Xcode是强大的IDE(但个人觉得不如Visual Studio做得好),其强大功能无需本人再赘述,本文也不是一篇“快捷键列表”,因为XCode上的快捷键极其多,而且还有不少是需要同时按下四个按键的 ...
- 探索C#之微型MapReduce
MapReduce近几年比较热的分布式计算编程模型,以C#为例简单介绍下MapReduce分布式计算. 阅读目录 背景 Map实现 Reduce实现 支持分布式 总结 背景 某平行世界程序猿小张接到B ...
- 决策树和基于决策树的集成方法(DT,RF,GBDT,XGBT)复习总结
摘要: 1.算法概述 2.算法推导 3.算法特性及优缺点 4.注意事项 5.实现和具体例子 内容: 1.算法概述 1.1 决策树(DT)是一种基本的分类和回归方法.在分类问题中它可以认为是if-the ...
- 替代jquery1.9版本以前的toggle事件函数(开关)
以上文章为转载自http://blog.sina.com.cn/s/blog_50042fab0101c7a9.html var flag=1; $(".selector").cl ...
- MongoDB 文档的查询和插入操作
MongoDB是文档型数据库,有一些专门的术语,和关系型DB相似,但也有差异,例如,Collection类似于关系型DB的Table,document类似于row,key/value pair类似于c ...
- Publication的 immediate_sync 属性
Publication的属性 immediate_sync 控制 Snapshot 文件的创建,如果属性 immediate_sync设置为true,那么snapshot file在snapshot ...
- SQL Server 解读【已分区索引的特殊指导原则】(1)- 索引对齐
一.前言 在MSDN上看到一篇关于SQL Server 表分区的文档:已分区索引的特殊指导原则,如果你对表分区没有实战经验的话是比较难理解文档里面描述的意思.这里我就里面的一些概念进行讲解,方便大家的 ...
- Unity3D知识框架
美术部分: 3d模型,材质,纹理,shader,Animator,Animation,天空盒,灯光效果,烘焙 程序部分: 基本组成: ...