iOS開發之自定義圓環式Slider
阿新 • • 發佈:2019-01-06
#pragma mark - UIControl functions //開始跟蹤觸控 -(BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { [super beginTrackingWithTouch:touch withEvent:event]; return YES; } //繼續跟蹤觸控 -(BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { [super continueTrackingWithTouch:touch withEvent:event]; CGPoint lastPoint = [touch locationInView:self]; [self moveHandle:lastPoint]; [self sendActionsForControlEvents:UIControlEventValueChanged]; return YES; } //結束跟蹤觸控 -(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { [super endTrackingWithTouch:touch withEvent:event]; if(_snapToLabels && labelsEvenSpacing != nil) { CGPoint bestGuessPoint; float minDist = 360; //距離 for (int i=0; i<[labelsEvenSpacing count]; i++) { //在圓上得百分比 CGFloat percentageAlongCircle = i/(float)[labelsEvenSpacing count]; //度數 CGFloat degreesForLabel = percentageAlongCircle * 360; if(abs(fixedAngle - degreesForLabel) < minDist) { minDist = abs(fixedAngle - degreesForLabel); bestGuessPoint = [self pointFromAngle:degreesForLabel + 90 + 180]; } } CGPoint centerPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); angle = floor(AngleFromNorth(centerPoint, bestGuessPoint, NO)); _currentValue = [self valueFromAngle]; [self setNeedsDisplay]; } }