这两天接手了一个外包的UI,有一个环形的控制器,需求改啊改的:“安卓已经实现了……”,最讨厌这句了,最后做了一版,对方终于满意了,删掉其他的繁琐部分,留下控制器部分,大家看看,有更好的想法欢迎分享。
惯例,先上图
源码在这里
给大家做个分析吧:
核心代码解析:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 2 { 3 UITouch *touch = [touches anyObject]; 4 CGPoint point = [touch locationInView:self.image];//取点 5 6 CGPoint pointerposition = [self getPoint:point];//加工点 7 double radian = [self getradian:pointerposition];//求弧度 8 if (radian>-0.81&&radian<3.99) {//根据弧度范围处理点的运动 9 _pointer.transform = CGAffineTransformMakeRotation(- (radian - M_PI_2)); 10 _pointer.center = pointerposition; 11 } 12 } 13 14 - (double)getradian:(CGPoint)point//利用点求弧度 15 { 16 point.x -= _center.x; 17 point.y -= _center.y; 18 double cosp = point.x / _radius;//求余弦 19 double radian = acos(cosp);//转弧度 20 radian = (radian < M_PI_2 && point.y > 0) ? - radian : radian;//判断象限,注意屏幕坐标系的方向 21 radian = (radian > M_PI_2 && point.y > 0) ? M_PI * 2 - radian : radian; 22 return radian; 23 } 24 25 - (CGPoint) getPoint:(CGPoint)point//将触摸点,转换成圆周上的点 26 { 27 point.x -= _center.x; 28 point.y -= _center.y;//以上两步为了将左边系的原点固定到圆心, 29 double r = sqrt(point.x * point.x + point.y * point.y) / _radius; 30 point.x /= r; 31 point.y /= r; 32 point.x += _center.x; 33 point.y += _center.y;//按照比例求出对应的圆上的点 34 return point; 35 }