iOS实现拖拽View跟随手指浮动效果
程序员文章站
2024-01-30 12:12:04
本文实例为大家分享了ios实现拖拽view跟随手指浮动的具体代码,供大家参考,具体内容如下效果图:1.自定义要跟随手指浮动的那个view//// orangeview.m// 拖拽view跟随手指浮动...
本文实例为大家分享了ios实现拖拽view跟随手指浮动的具体代码,供大家参考,具体内容如下
效果图:
1.自定义要跟随手指浮动的那个view
// // 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上去;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。