需求:
项目中要求做一个类似于iOS中AssistiveTouch的悬浮按钮,用于给用户展示广告。不同于AssistiveTouch的地方是我们的TouchView不会悬浮在上面或者下面,只会悬浮在左边或者右边。
实现:
新建一个TouchView,通过传入的maxEdge参数设置TouchView的移动范围。@property(nonatomic, assign)UIEdgeInsets maxEdge;//设置TouchView可以移动的最大范围
- (instancetype)initWithMaxEdge:(UIEdgeInsets)maxEdge;
初始化方法中保存maxEdge- (instancetype)initWithMaxEdge:(UIEdgeInsets)maxEdge {
self = [super init];
if (self) {
self.maxEdge = maxEdge;
}
return self;
}
触摸时开始移动时TouchView跟随手指位置移动- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//求偏移量 = 手指当前点的X - 手指上一个点的X
CGPoint currentPoint = [touch locationInView:self];
CGPoint prePoint = [touch previousLocationInView:s