本文实例为大家分享了ios实现拖拽view跟随手指浮动的具体代码,供大家参考,具体内容如下
效果图:
1.自定义要跟随手指浮动的那个view
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
//
// orangeview.m
// 拖拽view跟随手指浮动
//
// created by llkj on 2017/8/16.
// copyright © 2017年 laynecheung. all rights reserved.
//
#import "orangeview.h"
@implementation orangeview
//当开始触摸屏幕的时候调用
- ( void )touchesbegan:(nsset<uitouch *> *)touches withevent:(uievent *)event{
nslog(@ "%s" , __func__);
}
//触摸时开始移动时调用(移动时会持续调用)
//nsset:无序
//nsarray:有序
- ( void )touchesmoved:(nsset<uitouch *> *)touches withevent:(uievent *)event{
nslog(@ "%s" , __func__);
uitouch *touch = [touches anyobject];
//求偏移量 = 手指当前点的x - 手指上一个点的x
cgpoint currentpoint = [touch locationinview:self];
cgpoint prepoint = [touch previouslocationinview:self];
nslog(@ "ccurrentpoint = %@" , nsstringfromcgpoint(currentpoint));
nslog(@ "prepiont = %@" , nsstringfromcgpoint(prepoint));
cgfloat offsetx = currentpoint.x - prepoint.x;
cgfloat offsety = currentpoint.y - prepoint.y;
//平移
self.transform = cgaffinetransformtranslate(self.transform, offsetx, offsety);
}
//当手指离开屏幕时调用
-( void )touchesended:(nsset<uitouch *> *)touches withevent:(uievent *)event{
nslog(@ "%s" , __func__);
}
//当发生系统事件时就会调用该方法(电话打入,自动关机)
- ( void )touchescancelled:(nsset<uitouch *> *)touches withevent:(uievent *)event{
nslog(@ "%s" , __func__);
}
@end
|
2.创建自定义的view
在storyboard中拖一个view绑定他的类为orangeview;
或者代码创建手动添加到控制器的view上去;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u010981736/article/details/77309449