AI智能
改变未来

iOS实现拖拽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 - 手指上一个点的XCGPoint 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上去;

以上就是本文的全部内容,希望对大家的学习有所帮助

您可能感兴趣的文章:

  • iOS开发UICollectionView实现拖拽效果
  • IOS Xib控件拖拽与页面跳转实例
  • iOS实现百度地图拖拽后更新位置以及反编码
  • 浅谈iOS11新特性:新增拖拽交互体验
  • Android ReboundScrollView仿IOS拖拽回弹效果
  • IOS 实现3D 浮动效果动画
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » iOS实现拖拽View跟随手指浮动效果