iOS阶段学习第35天笔记(Touch手势介绍)

一、Touch手势

1、利用手势实现UIButton移动效果  实例代码
     
1) 创建一个继承自UIButton的类 MyButton.h  代码实现

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

2)MyButton.m  的代码实现

 1 #import "MyButton.h"
 2 @implementation MyButton
 3 {
 4     CGPoint _lastPoint;
 5 }
 6
 7 //手势开始
 8 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 9 {
10     UITouch *touch = [touches anyObject];
11     CGPoint point = [touch locationInView:self];
12     NSLog(@"began:%@",NSStringFromCGPoint(point));
13     _lastPoint = point;
14 }
15
16 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
17 {
18     UITouch *touch = [touches anyObject];
19     CGPoint point = [touch locationInView:self];
20     CGFloat offsetx = point.x - _lastPoint.x;
21     CGFloat offsety = point.y - _lastPoint.y;
22     self.center = CGPointMake(self.center.x + offsetx, self.center.y + offsety);
23     NSLog(@"moved:%@",NSStringFromCGPoint(point));
24 }
25
26 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
27 {
28     UITouch *touch = [touches anyObject];
29     CGPoint point = [touch locationInView:self];
30     NSLog(@"end:%@",NSStringFromCGPoint(point));
31 }
32 @end 

3)父视图中的代码实现

 1 #import "ViewController.h"
 2 #import "MyButton.h"
 3 @interface ViewController ()
 4 {
 5     MyButton *_v;
 6     CGPoint _lastPoint;
 7 }
 8 @end
 9
10 @implementation ViewController
11
12 - (void)viewDidLoad {
13     [super viewDidLoad];
14     _v = [[MyButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
15  _v.backgroundColor = [UIColor redColor];
16     [self.view addSubview:_v];
17 }
18
19 //手势开始
20 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
21 {
22     UITouch *touch = [touches anyObject];
23     CGPoint point = [touch locationInView:self.view];
24     NSLog(@"began:%@",NSStringFromCGPoint(point));
25     _lastPoint = point;
26 }
27
28 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
29 {
30     UITouch *touch = [touches anyObject];
31     CGPoint  point = [touch locationInView:self.view];
32     CGFloat  offsetx = point.x - _lastPoint.x;
33     CGFloat  offsety = point.y - _lastPoint.y;
34     _v.center = CGPointMake(_v.center.x + offsetx, _v.center.y + offsety);
35     _lastPoint = point;
36     NSLog(@"moved:%@",NSStringFromCGPoint(point));
37 }
38
39 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
40 {
41     UITouch *touch = [touches anyObject];
42     CGPoint  point = [touch locationInView:self.view];
43     NSLog(@"end:%@",NSStringFromCGPoint(point));
44 }
45
46 -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
47 {
48
49 }
50 @end

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

 1 #import "ViewController.h"
 2 //遵守旋转与缩放的代理协议
 3 @interface ViewController ()<UIGestureRecognizerDelegate>
 4 @end
 5
 6 @implementation ViewController
 7
 8 - (void)viewDidLoad {
 9     [super viewDidLoad];
10
11     UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
12     imgv.center = self.view.center;
13     [self.view addSubview:imgv];
14     imgv.image = [UIImage imageNamed:@"3"];
15     imgv.userInteractionEnabled = YES;
16
17     //点击手势
18     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGes:)];
19
20     //设置该手势需要的手指数
21     tap.numberOfTouchesRequired = 2;
22
23     //设置该手势的点击次数
24     tap.numberOfTapsRequired = 4;
25     [imgv addGestureRecognizer:tap];
26
27     //平移手势,拖拽手势
28     UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
29     [imgv addGestureRecognizer:pan];
30
31     //缩放手势
32     UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGes:)];
33     [imgv addGestureRecognizer:pinch];
34     pinch.delegate = self;
35
36     //旋转
37     UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)];
38     [imgv addGestureRecognizer:rotation];
39     rotation.delegate = self;
40 }
41
42 //返回值表示能否同时识别其他(相对于已经设置了代理的手势)手势
43 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:     (UIGestureRecognizer *)otherGestureRecognizer
44 {
45     return YES;
46 }
47 -(void)rotationGes:(UIRotationGestureRecognizer *)rotation
48 {
49     rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
50     rotation.rotation = 0.0;
51 }
52 -(void)pinchGes:(UIPinchGestureRecognizer *)pinch
53 {
54     //transform:仿射变换
55     //pinch.scale,是缩放手势的捏合倍率
56     pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
57
58     //倍率还原
59     pinch.scale = 1.0;
60 }
61 -(void)panGes:(UIPanGestureRecognizer *)pan
62 {
63     //返回当前的手势的偏移量
64     CGPoint offset = [pan translationInView:pan.view];
65     //pan.view就是pan手势所加到的视图
66     pan.view.center = CGPointMake(pan.view.center.x + offset.x, pan.view.center.y + offset.y);
67     //移动以后,把偏移量归0
68     [pan setTranslation:CGPointZero inView:pan.view];
69 }
70
71 -(void)tapGes:(UIGestureRecognizer *)tap
72 {
73     NSLog(@"==========");
74 }
75 @end
时间: 2024-08-08 13:56:46

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

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

iOS学习(UI)知识点整理 一.关于UIButton的介绍 1)概念:UIButton 是一种常用的控件,通过点击触发相应的功能 2)UIButton 的几种常用的状态        1.UIControlStateNormal  正常状态        2.UIControlStateHighlighted 高亮状态        3.UIControlStateSelected 选中状态  -> 当button的selected设置成yes之后才能触发 3)UIButton常用的几种事件 

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

iOS学习(UI)知识点整理 一.关于UITextField的介绍 1)概念: UITextField 是用于接收用户输入的一个控件 2)UITextField  初始化实例代码: 1 //创建一个UItextField实例 2 UITextField *textField = [[UITextField alloc] init]; 3 textField.frame = CGRectMake(10, 40, self.view.frame.size.width - 20, 40); 4 tex

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

iOS学习(UI)知识点整理 一.关于UIVIew 的介绍 1)概念:UIView 是用于装载并展示各类控件的大容器,是iOS中所有UI控件的基类 2)UIView  初始化实例代码 1 UIView *view1 = [[UIView alloc] init]; 2 view1.frame = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height/2); 3 view1.backgroundColor

IOS阶段学习第四天笔记(循环)

    IOS学习(C语言)知识点整理笔记 一.分支结构 1.分支结构分为单分支 即:if( ){ } ;多分支 即:if( ){ }else{ }  两种 2.单分支 if表达式成立则执行{ }里的语句:双分支 if表达式不成立 则执行else{ }里面的语句 3.字符串的输出系统会从数组地址一直打印到字符 ‘\0’为止,如果没有正确初始化,可能会打印出数组外的信息 4.如果分支结构里面只有一条执行语句可省略外面的大括号{} . 二.开关语句 1.语句结构 :switch (参数) case

IOS阶段学习第三天笔记(运算符)

                                         IOS学习(C语言)知识点整理笔记 1.运算符 一.算术运算符 1)表达式由变量.常量.运算符构成,有确定的类型和值 2)算术运算符包括: +(加),-(减),*(乘),/(除),%(模) 3)算术运算符优先级 括号()> * ,/ ,%  >+,- 4)%表示取余.取模  a%b 表示a除以b取余数 5)整数相除保留两位小数处理方法如: printf(“%.2f”,(float)14/9); 6)自增自减运算符

IOS 阶段学习第24天笔记(Block的介绍)

IOS学习(OC语言)知识点整理 一.Block 的介绍 1)概念: block 是一种数据类型,类似于C语言中没有名字的函数,可以接收参数,也可以返回值与C函数一样被调用 封装一段代码 可以在任何地方调用 block 也可以作为函数参数,以及函数返回值 2)Block 实例代码 1 //定义了一个block类型MyBlock,MyBlock类型的变量只能指向带两个int的参数和返回int的代码块 2 typedef int (^MyBlock)(int,int); 3 //定义一个函数指针 4

IOS 阶段学习第23天笔记(XML数据格式介绍)

IOS学习(OC语言)知识点整理 一.XML数据格式介绍 1)概念:xml是extensible markup language扩展的标记语言,一般用来表示.传输和存储数据 2)xml与json目前使用比较广泛的两种网络传输数据格式 两者分别占比: 1. json:市场上占90%,轻量级的表示数据 2.xml:占10%,表示数据比较复杂 3)XML三种数据解析方法: 1.DOM解析:将整个xml数据加载到内存中,构造一个对象,从根结点开始一级一级的解析提取数据.缺点:如果数据大, 比较占内存,解

iOS阶段学习第34天笔记(UI小组件 UISegment-UISlider-UIStepper-UIProgressView-UITextView介绍)

iOS学习(UI)知识点整理 一.UI小组件 1.UISegmentedControl 分段选择器  实例代码 1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 //分段选择器 4 //在iOS6里,每个段的宽度会根据字数来决定 5 //iOS7以后,每个段的宽度相同,宽度取决于最长的字数 6 self.view.backgroundColor=[UIColor whiteColor]; 7 seg=[[UISegmentedControl all

IOS 阶段学习第十天笔记(结构体)

IOS学习(C语言)知识点整理 一.数据结构 1)概念:数据结构是指计算机程序中所操作的对象——数据以及数据元素之间的相互关系和运算. 2)结构体必须有struct 关键字修饰. 实例代码: 1 struct Person{ 2 char name[20]; 3 int age; 4 float height; 5 }; 6 7 int main(){ 8 struct Person Tom={"Tom",22,180};//struct Person 是数据结构 9 //Tom 是变