服务器之家

服务器之家 > 正文

iOS实现拖拽View跟随手指浮动效果

时间:2021-05-31 16:31     来源/作者:LayneCheung

本文实例为大家分享了ios实现拖拽view跟随手指浮动的具体代码,供大家参考,具体内容如下

效果图:

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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部