zoukankan      html  css  js  c++  java
  • iOS环形控制器、环形按钮

    这两天接手了一个外包的UI,有一个环形的控制器,需求改啊改的:“安卓已经实现了……”,最讨厌这句了,最后做了一版,对方终于满意了,删掉其他的繁琐部分,留下控制器部分,大家看看,有更好的想法欢迎分享。

    惯例,先上图

    源码在这里

    给大家做个分析吧:

    核心代码解析:

     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 }
    View Code
  • 相关阅读:
    SpringBoot HATEOAS用法简介
    犀函
    dubbo 相关面试题 有用(转)
    想使用消息队列,先考虑下这些问题!
    appium在Mac上环境搭建
    3. SOFAJRaft源码分析— 是如何进行选举的?
    Redis相关知识
    替代微信ipad协议(转)
    c#面试题(1)(转)
    例题6-5 Boxes in a line uVa12657
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/4966599.html
Copyright © 2011-2022 走看看