欢迎光临
我们一直在努力

模拟温控器 圆环手动滚动

建站超值云服务器,限时71元/月

模拟温控器 圆环手动滚动

项目介绍:

  1. @interface SXCircleTempView : UIView
  2. @property(assign,nonatomic) NSInteger circlyValue;
  3. // 一共分为多少份
  4. @property(assign,nonatomic) CGFloat totalValue;
  5. // 每一份 基本值设置多少(如0.5,1,2)等
  6. @property(assign,nonatomic) CGFloat cellValue;
  7. // 用户设置等温度是多少
  8. @property(copy,nonatomic) void (^circelTempSetTempBlock)(NSInteger selectValue);
  9. – (void)setCurrentTempString:(CGFloat)currentTemp;
  10. @end
  11. #import "UILabel+SXExtention.h"
  12. #import "Masonry.h"
  13. // 初始低 角度
  14. #define STARTANYGLE  ((float)M_PI)/7
  15. @interface SXCircleTempView()
  16. // 大圆半径
  17. @property(assign,nonatomic) CGFloat radious;
  18. // 偏离 的基本单位
  19. @property(assign,nonatomic) CGFloat cellAnygle;
  20. // 记住 滑动以后 偏离多少度
  21. @property(assign,nonatomic) CGFloat totalCellTotalAngle;
  22. @property(assign,nonatomic) CGPoint selfCenter;
  23. @property(assign,nonatomic) CGFloat lineWidth;
  24. // 小球view 的frame值
  25. @property(assign,nonatomic) CGRect valveRect;
  26. // 是否触摸在小球上
  27. @property(assign,nonatomic) BOOL spot;
  28. //圆弧View
  29. @property(weak,nonatomic) UIView *circleView;
  30. // 减去温度Btn
  31. @property(weak,nonatomic) UIButton *mulBtn;
  32. // 添加Btn
  33. @property(weak,nonatomic) UIButton *addButton;
  34. // 显示多少度温度
  35. @property(weak,nonatomic) UILabel *setTempValueLabel;
  36. // 显示当前温度
  37. @property(weak,nonatomic) UILabel *currentTemp;
  38. @end
  39. @implementation SXCircleTempView
  40. – (instancetype)initWithFrame:(CGRect)frame{
  41.     if (self = [super initWithFrame:frame]) {
  42.        // _cellAnygle = (2 * ((float)M_PI)- STARTANYGLE * 5)/100;
  43.         _totalValue = 100;
  44.         _circlyValue = 20;
  45.         _cellValue = 0.5;
  46.         self.lineWidth = 3;
  47.         [self addUIView];
  48.     }
  49.     return self;
  50. }
  51. – (void)setTotalValue:(CGFloat)totalValue{
  52.     _totalValue = totalValue;
  53.     _cellAnygle = (2 * ((float)M_PI)- STARTANYGLE * 5)/totalValue;
  54. }
  55. – (void)drawRect:(CGRect)rect {
  56.     [self drawSetUPUI];
  57. }
  58. – (void)setCirclyValue:(NSInteger)circlyValue{
  59.     _circlyValue  = circlyValue;
  60.     self.setTempValueLabel.text = [NSString stringWithFormat:@"%.1f",circlyValue*(_cellValue?_cellValue:0.5)];
  61.     [self setNeedsDisplay];
  62. }
  63. – (void)drawSetUPUI{
  64.     // 画大圆
  65.   //  CGFloat lineWidth =  3;
  66.     CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
  67.     CGFloat radius = (self.bounds.size.width – _lineWidth)/2 – 5;
  68.     self.selfCenter = center;
  69.     _radious = radius;
  70.     CGFloat startAngle = STARTANYGLE; //
  71.     CGFloat endAngle = ((float)M_PI) – startAngle;
  72.     CAShapeLayer *layer = [[CAShapeLayer alloc] init];
  73.     layer.name = @"Radius";
  74.     UIBezierPath *path = [UIBezierPath bezierPath];
  75.     path.lineWidth = _lineWidth;
  76.     path.lineCapStyle = kCGLineCapButt;
  77.     [[UIColor lightGrayColor] set];
  78.     [path addArcWithCenter:center radius:radius startAngle:endAngle endAngle:startAngle clockwise:YES];
  79.     [path stroke];
  80.     [path closePath];
  81.     CGFloat grayStartAngle =M_PI – STARTANYGLE; //
  82.     CGFloat grayEndAngle = _cellAnygle * _circlyValue + grayStartAngle;//((float)M_PI) – grayStartAngle;
  83.     CAShapeLayer *grayLayer = [[CAShapeLayer alloc] init];
  84.     grayLayer.name = @"Radius";
  85.     UIBezierPath *grayPath = [UIBezierPath bezierPath];
  86.     grayPath.lineWidth = _lineWidth;
  87.     grayPath.lineCapStyle = kCGLineCapButt;
  88.     [[UIColor whiteColor] set];
  89.     [grayPath addArcWithCenter:center radius:radius startAngle:grayStartAngle endAngle:grayEndAngle clockwise:YES];
  90.     [grayPath stroke];
  91.     [grayPath closePath];
  92.     CGFloat circelViewWidth = _lineWidth * 4;
  93.     CGFloat pi_mYAngle = _cellAnygle * _circlyValue – startAngle;
  94.     CGFloat circleViewX = 0;
  95.     CGFloat circleViewY = 0;
  96.     circleViewX = (_radious – _radious * cos(pi_mYAngle)) + _lineWidth – _lineWidth/2.0;
  97.     circleViewY = _radious – _radious * sin(pi_mYAngle) + _lineWidth – _lineWidth/2.0;
  98.     self.valveRect = CGRectMake(circleViewX, circleViewY, circelViewWidth, circelViewWidth);
  99.     self.circleView.frame = self.valveRect;
  100. }
  101. – (void)addUIView{
  102.     CGFloat maging = 5;
  103.     CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
  104.     CGFloat radius = (self.bounds.size.width – _lineWidth)/2 – maging;
  105.     self.selfCenter = center;
  106.     _radious = radius;
  107.     CGFloat startAngle = STARTANYGLE; //
  108.   //  CGFloat endAngle = ((float)M_PI) – startAngle;
  109.     CGFloat pi_mYAngle = _cellAnygle * _circlyValue – startAngle;
  110.     CGFloat circleViewX = 0;
  111.     CGFloat circleViewY = 0;
  112.     circleViewX = (_radious – _radious * cos(pi_mYAngle)) + _lineWidth – _lineWidth/2.0;
  113.     circleViewY = _radious – _radious * sin(pi_mYAngle) + _lineWidth – _lineWidth/2.0;
  114.     CGFloat circelViewWidth = _lineWidth * 4;
  115.     circleViewX = (_radious – _radious * cos(pi_mYAngle)) + _lineWidth – _lineWidth/2.0;
  116.     circleViewY = _radious – _radious * sin(pi_mYAngle) + _lineWidth – _lineWidth/2.0;
  117.     self.valveRect = CGRectMake(circleViewX, circleViewY, circelViewWidth, circelViewWidth);
  118.     UIView *circleView = [[UIView alloc] initWithFrame:self.valveRect];
  119.     self.circleView = circleView;
  120.     circleView.layer.cornerRadius = circelViewWidth/2.0;
  121.     circleView.layer.masksToBounds = YES;
  122.     circleView.userInteractionEnabled = NO;
  123.     [self addSubview:circleView];
  124.     circleView.backgroundColor = [UIColor whiteColor];
  125.     CGFloat btnWidth = 30;
  126.     CGFloat mulLeftY = radius + radius * sin(STARTANYGLE) + 2;
  127.     CGFloat mulLeftX = maging * 2;
  128.     UIButton *mulBtn = [[UIButton alloc] initWithFrame:CGRectMake(mulLeftX, mulLeftY + _lineWidth, btnWidth, btnWidth)];
  129.     [mulBtn addTarget:self action:@selector(addOrmulBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  130.     [self btnSetComm:mulBtn content:@"-"];
  131.     self.mulBtn = mulBtn;
  132.     [self addSubview:mulBtn];
  133.     //    UIView *autView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
  134.     // autView.backgroundColor = [UIColor redColor];
  135.     UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width – mulLeftX – btnWidth, mulLeftY + _lineWidth, btnWidth, btnWidth)];
  136.     [addBtn addTarget:self action:@selector(addOrmulBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  137.     self.addButton = addBtn;
  138.     [self btnSetComm:addBtn content:@"+"];
  139.     [self addSubview:addBtn];
  140.     UILabel *setTempValueLabel = [UILabel label:@"29.5" font:40 textColor:[UIColor whiteColor]];
  141.     setTempValueLabel.textAlignment = NSTextAlignmentCenter;
  142.     self.setTempValueLabel = setTempValueLabel;
  143.   //  setTempValueLabel.center = self.selfCenter;
  144.   //  [setTempValueLabel sizeToFit];
  145.     [self addSubview:setTempValueLabel];
  146.     [setTempValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  147.         make.centerX.equalTo(self.mas_centerX);
  148.         make.centerY.equalTo(self.mas_centerY).offset(-10);
  149.     }];
  150.     UILabel *tempSimbleLabel = [UILabel label:@"°C" font:13 textColor:[UIColor whiteColor]];
  151.     [self addSubview:tempSimbleLabel];
  152.     [tempSimbleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  153.         make.top.equalTo(setTempValueLabel.mas_top).offset(10);
  154.         make.left.equalTo(setTempValueLabel.mas_right);
  155.     }];
  156.     UILabel *tipSetTempLabel = [UILabel label:@"设置温度" font:14 textColor:[UIColor whiteColor]];
  157.     tipSetTempLabel.textAlignment = NSTextAlignmentCenter;
  158.     [self addSubview:tipSetTempLabel];
  159.     [tipSetTempLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  160.         make.bottom.equalTo(setTempValueLabel.mas_top);
  161.         make.centerX.equalTo(setTempValueLabel.mas_centerX);
  162.     }];
  163.     UILabel *currentLabel = [UILabel label:@"当前温度:25.3°C" font:12 textColor:[UIColor whiteColor]];
  164.     self.currentTemp = currentLabel;
  165.     [self addSubview:currentLabel];
  166.     [currentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  167.         make.top.equalTo(setTempValueLabel.mas_bottom);
  168.         make.centerX.equalTo(setTempValueLabel.mas_centerX);
  169.     }];
  170. }
  171. – (void)addOrmulBtnClick:(UIButton *)sender{
  172.     if (sender == self.addButton) {
  173.         if (self.circlyValue >= _totalValue) {
  174.             self.circlyValue = _totalValue;
  175.         }else{
  176.             self.circlyValue = self.circlyValue + 1;
  177.         }
  178.     }else{
  179.         if (self.circlyValue <= 0) {
  180.             self.circlyValue = 0;
  181.         }else{
  182.             self.circlyValue = self.circlyValue – 1;
  183.         }
  184.     }
  185. }
  186. – (void)btnSetComm:(UIButton *)sender content:(NSString *)content{
  187.     [sender setTitle:content forState:UIControlStateNormal];
  188.     sender.backgroundColor = [UIColor whiteColor];
  189.     sender.layer.cornerRadius = sender.frame.size.width/2.0;
  190.     sender.clipsToBounds = YES;
  191.     sender.titleLabel.font = [UIFont systemFontOfSize:30];
  192.     [sender setTitleColor:[UIColor colorWithRed:59/255.0 green:139/255.0 blue:231/255.0 alpha:1] forState:UIControlStateNormal];
  193.     sender.backgroundColor = [UIColor whiteColor];
  194. }
  195. // 开始触摸
  196. – (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  197.     UITouch *touch = [touches anyObject];
  198.     CGPoint point = [touch locationInView:self];
  199.     point = CGPointMake(point.x, point.y);
  200.     CGFloat temOffset = 15;
  201.     CGRect toucheTect = CGRectMake(self.valveRect.origin.x – temOffset, self.valveRect.origin.y – temOffset, self.valveRect.size.width + temOffset * 2.0, self.valveRect.size.height + temOffset * 2.0);
  202.     BOOL container =  CGRectContainsPoint(toucheTect, point);
  203.     if (container) {
  204.         self.spot = YES;
  205.        // NSLog(@"dlfdlasfdljfaldsjfalfjdlsajfljfaljf");
  206.     }
  207.     else
  208.     {
  209.         self.spot = NO;
  210.     }
  211. }
  212. // 开始滑动
  213. – (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  214.     UITouch *touch = [touches anyObject];
  215.     CGPoint point = [touch locationInView:self];
  216.     point = CGPointMake(point.x, point.y + 0);
  217.     CGFloat radious = _radious;
  218.     CGFloat XX = (_selfCenter.x – point.x) * (_selfCenter.x – point.x);
  219.     CGFloat YY = (_selfCenter.y – point.y) * (_selfCenter.y – point.y);
  220.     CGFloat OutLim = (radious ) * (radious);
  221.     CGFloat inLim = (radious – 20) * (radious – 20);
  222.     if (XX + YY < OutLim && XX + YY > inLim && (point.y < self.radious + self.radious * sin(STARTANYGLE) )) {
  223.         if (self.spot) {
  224.             //(2 * ((float)M_PI)- STARTANYGLE * 5)
  225.             BOOL isBig = point.x > _selfCenter.x?M_PI_2:0;
  226.             CGFloat roundAngle;
  227.             roundAngle = isBig ? (2 * ((float)M_PI)- STARTANYGLE * 5) –  asinf((_selfCenter.y – point.y)/_radious) –  STARTANYGLE: asinf((_selfCenter.y – point.y)/_radious) + STARTANYGLE;
  228.             self.circlyValue = (NSInteger)(round(roundAngle/_cellAnygle));
  229.         }
  230.         return;
  231.     }
  232. }
  233. – (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  234. {
  235.     self.spot = NO;
  236.   //  NSLog(@"拖拽结束=========");
  237. }
  238. – (void)setCurrentTempString:(CGFloat)currentTemp{
  239.     self.currentTemp.text = [NSString stringWithFormat:@"当前温度:%.1f°C",currentTemp];
  240. }
  241. @end
  242. @interface UILabel (SXExtention)
  243. + (UILabel *) label:(NSString *)title frame:(CGRect)frame font:(CGFloat)font textColor:(UIColor *)color;
  244. + (UILabel *) label:(NSString *)title font:(CGFloat)font textColor:(UIColor *)color;
  245. @end
  246. #import "UILabel+SXExtention.h"
  247. @implementation UILabel (SXExtention)
  248. + (UILabel *) label:(NSString *)title frame:(CGRect)frame font:(CGFloat)font textColor:(UIColor *)coloer{
  249.     UILabel *label = [[UILabel alloc] initWithFrame:frame];
  250.     label.text = title;
  251.     label.numberOfLines = 0;
  252.     label.textColor = coloer;
  253.     label.font = [UIFont systemFontOfSize:(font >= 0 ? font:14)];
  254.     return label;
  255. }
  256. + (UILabel *) label:(NSString *)title font:(CGFloat)font textColor:(UIColor *)color{
  257.     UILabel *label = [[UILabel alloc] init];
  258.     label.text = title;
  259.     label.textColor = color;
  260.     label.numberOfLines = 0;
  261.     label.font = [UIFont systemFontOfSize:(font >= 0 ? font:14)];
  262.     return label;
  263. }
  264. @end

复制代码

使用的时候要倒入 masonry框架
项目demo 下载地址:https://gitee.com/xiange/SXCircleTempViewDemo.git



赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 模拟温控器 圆环手动滚动
分享到: 更多 (0)