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)算术运 ...
随机推荐
- 删除Mysql数据表中多余的重复记录的sql语句
数据表 sniper_tb 中存在主键 id,字段url,现需要在url字段上添加 unique,但由于url存在重复记录,导致添加失败. 如何删除表中多余的url重复记录,仅保持一条? 思路一 将 ...
- .NET中那些所谓的新语法之三:系统预定义委托与Lambda表达式
开篇:在上一篇中,我们了解了匿名类.匿名方法与扩展方法等所谓的新语法,这一篇我们继续征程,看看系统预定义委托(Action/Func/Predicate)和超爱的Lambda表达式.为了方便码农们,. ...
- angularjs 指令详解 - template, restrict, replace
通过指令机制,angularjs 提供了一个强大的扩展系统,我们可以通过自定义指令来扩展自己的指令系统. 怎样定义自己的指令呢? 我们通过 Bootstrap UI来学习吧.这个项目使用 angula ...
- 探索C#之微型MapReduce
MapReduce近几年比较热的分布式计算编程模型,以C#为例简单介绍下MapReduce分布式计算. 阅读目录 背景 Map实现 Reduce实现 支持分布式 总结 背景 某平行世界程序猿小张接到B ...
- EasyPR--一个开源的中文车牌识别系统
我正在做一个开源的中文车牌识别系统,Git地址为:https://github.com/liuruoze/EasyPR. 我给它取的名字为EasyPR,也就是Easy to do Plate Reco ...
- .Net组件程序设计之远程调用(一)
.Net组件程序设计之远程调用(一) 1应用程序域 我们知道我们写的C#代码是在操作系统逻辑体系结构中最上层的,然而操作系统本身是不会认识C#代码的,它只认识机器代码.那我们写的程序经过编译后是编译成 ...
- Android 解决方法数 65536 (65k) 限制
可能出现的错误信息: Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: ...
- struts1二:基本环境搭建
首先建立一个web项目 引入需要的jar包 建立包com.bjpowernode.struts创建LoginAction package com.bjpowernode.struts; import ...
- for 循环打印图形
public class For { public static void main(String[] args) { //"使用双层for循环打印图形时,外层管行,内层管列",那 ...
- Spring MVC和CXF集成
前提: 1.spring mvc环境已搭建好,能跑起来. 2.下载apache-cxf-2.7.3.zip的压缩包,解压apache-cxf-2.7.3.zip压缩包,拷贝如下几个jar包即可. 配置 ...