需求
最近工作中有个对完成动作难度评价的视图,设计给出了以下设计方案:
![](https://img.haomeiwen.com/i2176862/d4fb5da6a33925c9.png)
要求:
1、拖动选中
2、点击选中
3、拖动视图和文字跟随颜色变化
开始
① 我的想法是既能滑动又能点击,可以使用UIControl自定义视图,在以下四个方法中处理手势事件:
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;
- (void)endTrackingWithTouch:(nullable UITouch *)touch withEvent:(nullable UIEvent *)event;
- (void)cancelTrackingWithEvent:(nullable UIEvent *)event;
② 渐变颜色可以使用CAGradientLayer,滑动过程中,识别颜色视图中的色值;
③ 点击处理:在 endTrackingWithTouch
中,处理最终停留的位置。
④ 在代理
实现效果
![](https://img.haomeiwen.com/i2176862/3cf4a67fef6bc391.gif)
代码部分
一、使用
![](https://img.haomeiwen.com/i2176862/742bc7b3829ee0f8.png)
![](https://img.haomeiwen.com/i2176862/cfe0d78f51e51d82.png)
二、视图结构
.h文件中暴露了一些外部可修改的可选属性。
![](https://img.haomeiwen.com/i2176862/3dd33318057ea52b.png)
选择完成后,通过代理方法返回下标和当前选中的颜色色值
![](https://img.haomeiwen.com/i2176862/f3a84edc472a84c2.png)
网友评论