当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS实现拖拽View跟随手指浮动效果

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

2020年05月14日  | 移动技术网IT编程  | 我要评论

风声鹤唳txt下载,免费高清伦理电影,四川阿坝州阿坝县藏文中学

本文实例为大家分享了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上去;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网