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 *)touches withEvent:(UIEvent *)event{NSLog(@"%s", __func__);
}//触摸时开始移动时调用(移动时会持续调用)
//NSSet:无序
//NSArray:有序
- (void)touchesMoved:(NSSet *)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 *)touches withEvent:(UIEvent *)event{NSLog(@"%s", __func__);
}//当发生系统事件时就会调用该方法(电话打入,自动关机)
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{NSLog(@"%s", __func__);
}
@end
2.创建自定义的View 在storyboard中拖一个View绑定他的类为OrangeView;
或者代码创建手动添加到控制器的View上去;
推荐阅读
- 2020-04-07vue中Axios的封装和API接口的管理
- iOS中的Block
- 列出所有自定义的function和view
- tableView|tableView 头视图下拉放大 重写
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- Flutter的ListView
- 接口|axios接口报错-参数类型错误解决