iPhone – 如何做双圆形滑块(时钟像功能)
发布时间:2020-12-14 17:12:22 所属栏目:百科 来源:网络整理
导读:我想知道如何在附加图像中执行双滑块. 我正在看这个代码来修改它.我想知道如何让2个滑块允许用户选择所需的时间. 我遇到的问题是我如何有2个滑块来显示像图像? http://www.cocoacontrols.com/controls/tb_circularslider 任何评论都非常感谢. 解决方法 对于
我想知道如何在附加图像中执行双滑块.
我正在看这个代码来修改它.我想知道如何让2个滑块允许用户选择所需的时间. 我遇到的问题是我如何有2个滑块来显示像图像? http://www.cocoacontrols.com/controls/tb_circularslider 任何评论都非常感谢. 解决方法
对于双滑块位置,您可以在代码中使用此摘录
CGContextAddArc(imageCtx,self.frame.size.width/2,self.frame.size.height/2,radius,ToRad(self.angle),0); 第一个零(0)是起点,所以你想在这里使用不同的角度 CGContextAddArc(imageCtx,ToRad(self.startAngle),ToRad(self.endAngle),0); (你当然需要你头上的那两个ivar) 编辑:这是编辑的代码,用于查找最近的旋钮并将其锁定以进行修改.旧代码没有锁定它,因此它会在从一个旋钮悬停到另一个旋钮时发生变化. enum SliderLockType { SliderLockedNone = 0,SliderLockedStart,SliderLockedEnd }; #pragma mark - Implementation - @implementation TBCircularSlider enum SliderLockType sliderLock; // … some code here … //Initialize the Angle at 0 //self.startAngle = 0; //self.endAngle = 270; /** Tracking is started **/ -(BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{ [super beginTrackingWithTouch:touch withEvent:event]; // find nearest knob … CGPoint lastPoint = [touch locationInView:self]; CGPoint pStart = [self centerPointFromAngel:self.startAngle]; CGPoint pEnd = [self centerPointFromAngel:self.endAngle]; float diffA = [self distanceBetween:lastPoint and:pStart]; float diffB = [self distanceBetween:lastPoint and:pEnd]; // … and lock it if (diffA <= TB_LINE_WIDTH) { // the tolerance is the width of the circle sliderLock = SliderLockedStart; } else if (diffB <= TB_LINE_WIDTH) { sliderLock = SliderLockedEnd; } //We need to track continuously return YES; } // continueTrackingWithTouch:withEvent: stays unchanged /** Track is finished **/ -(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{ [super endTrackingWithTouch:touch withEvent:event]; // reset the lock before starting a new touch event sliderLock = SliderLockedNone; } - (CGPoint)centerPointFromAngel:(int)angleInt { CGPoint point = [self pointFromAngle:angleInt]; point.x += TB_LINE_WIDTH/2; point.y += TB_LINE_WIDTH/2; return point; } - (CGFloat)distanceBetween:(CGPoint)p1 and:(CGPoint)p2 { CGFloat xDist = (p2.x - p1.x); CGFloat yDist = (p2.y - p1.y); return sqrt((xDist * xDist) + (yDist * yDist)); } // … some more code … - (void)drawTheHandle:(CGContextRef)ctx { CGContextSaveGState(ctx); //I Love shadows CGContextSetShadowWithColor(ctx,CGSizeMake(0,0),3,[UIColor blackColor].CGColor); //Get the handle position! CGPoint handleCenterA = [self pointFromAngle: self.startAngle]; CGPoint handleCenterB = [self pointFromAngle: self.endAngle]; //Draw It! [[UIColor colorWithWhite:1.0 alpha:0.7]set]; CGContextFillEllipseInRect(ctx,CGRectMake(handleCenterA.x,handleCenterA.y,TB_LINE_WIDTH,TB_LINE_WIDTH)); CGContextFillEllipseInRect(ctx,CGRectMake(handleCenterB.x,handleCenterB.y,TB_LINE_WIDTH)); CGContextRestoreGState(ctx); } - (void)movehandle:(CGPoint)lastPoint { //Get the center CGPoint centerPoint = CGPointMake(self.frame.size.width/2,self.frame.size.height/2); //Calculate the direction from the center point to an arbitrary position. float currentAngle = AngleFromNorth(centerPoint,lastPoint,NO); int angleInt = 360 - floor(currentAngle); if (sliderLock == SliderLockedStart) { self.startAngle = angleInt; } else if (sliderLock == SliderLockedEnd) { self.endAngle = angleInt; } //Redraw [self setNeedsDisplay]; } 结果: EDIT2:如果您希望滑块从小时切换到小时,您可以修改movehandle:方法,如下所示: int angleInt = (int)(360 - floor(currentAngle)) / 30 * 30; // 360/30 = 12 -> hours if (sliderLock == SliderLockedStart && angleInt%360 != self.endAngle%360) { self.startAngle = angleInt; } else if (sliderLock == SliderLockedEnd && angleInt%360 != self.startAngle%360) { self.endAngle = angleInt; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |