I am working on an application for an alarm clock in which I have to display an analog clock. The functionality is to set the time by clicking on the circumference of a circle displayed as a clock face.
I did it too. But the problem goes at 6 hours or 30 minutes. The angle is nan
for what makes my smooth watch completely cool.
Here is the code.
myPath1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(5, 5, 244, 244)]; [myPath1 closePath]; [myPath1 setLineWidth:20]; // Draw a shape Layer with Triangle Path CAShapeLayer *shapeLayer1 = [CAShapeLayer layer]; shapeLayer1.fillColor = [UIColor clearColor].CGColor; shapeLayer1.strokeColor = [UIColor clearColor].CGColor; shapeLayer1.lineWidth = 20; shapeLayer1.path = myPath1.CGPath; // Add it to your view [circleView.layer addSublayer:shapeLayer1]; -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:circleView]; if([self containsPointOnStroke:currentPoint onPath:myPath1]) { [self calculateAngle:currentPoint]; } } - (BOOL)containsPointOnStroke:(CGPoint)point onPath:(UIBezierPath *)path { CGPathRef cgPath = path.CGPath; CGPathRef strokedPath = CGPathCreateCopyByStrokingPath(cgPath, NULL, 10, kCGLineCapRound, kCGLineJoinRound, 1); BOOL pointIsNearPath = CGPathContainsPoint(strokedPath, NULL, point, NO); CGPathRelease(strokedPath); return pointIsNearPath; } //---- calculate angle from the point of touch taking center as the reference ----// - (void) calculateAngle : (CGPoint)currentPoint { double distance = sqrt(pow((currentPoint.x - 127), 2.0) + pow((currentPoint.y - 5), 2.0)); float val = ((2 * 14884) - powf((distance), 2)) / (2 * 14884); float angleVal = acosf(val) * 180/M_PI; float timeval = angleVal; //NSLog(@"angleVal : %f timeVal : %.2f", angleVal, timeval); if (isnan(angleVal)) { // if the value of the angle val is infinite if(check == 0) [hourBtn setTitle:@"06" forState:UIControlStateNormal]; else [minBtn setTitle:@"30" forState:UIControlStateNormal]; } else { if(check == 0) { if (currentPoint.x >= 127) [hourBtn setTitle:[self getHour:timeval reverseTIme:NO] forState:UIControlStateNormal]; else [hourBtn setTitle:[self getHour:timeval reverseTIme:YES] forState:UIControlStateNormal]; } else { if (currentPoint.x >= 127) [minBtn setTitle:[self getMin:timeval reverseTIme:NO] forState:UIControlStateNormal]; else [minBtn setTitle:[self getMin:timeval reverseTIme:YES] forState:UIControlStateNormal]; } } [timeLbl setText:[NSString stringWithFormat:@"%02d:%02d",[hourBtn.titleLabel.text intValue], [minBtn.titleLabel.text intValue]]]; } //---- get the hour time from the angle ----// - (NSString *) getHour :(float) angle reverseTIme:(BOOL) reverseCheck { int minuteCount = angle * 2; int quo = minuteCount / 60; int temp = quo; if(quo == 0) quo = 12; NSString *timeStr = [NSString stringWithFormat:@"%02d", quo]; quo = temp; if (reverseCheck) { int revQuo = 11 - quo; if(revQuo == 0) revQuo = 12; timeStr = [NSString stringWithFormat:@"%02d", revQuo]; } return timeStr; } // get the min time from the angle - (NSString *) getMin :(float) angle reverseTIme:(BOOL) reverseCheck { int minuteCount = angle; int rem = minuteCount / 6; NSString *timeStr = [NSString stringWithFormat:@"%02d", rem]; if (reverseCheck) { int revRem = 60 - rem; if(revRem == 60) revRem = 0; timeStr = [NSString stringWithFormat:@"%02d", revRem]; } //NSLog(@"timeStr : %@", timeStr); return timeStr; }
source share