2013-04-25 3 views
1

У меня возникли трудности с пониманием математики позади того, как рассчитать окружность узлов к центру круга. Надеюсь, вы, ребята, можете дать мне какой-нибудь указатель.как сдвинуть слайдер изображение узла за пределами

текущий расчет установить узел IMG в середине пирога, хотел бы перенести его ближе к внешнему кругу, как в IMG 2

спасибо за просмотр и комментирование, любые комментарии приветствуются.

enter image description here

как я тусклый ему быть. enter image description here

/** Draw a white knob over the circle **/ 
-(void) drawTheHandle:(CGContextRef)ctx{ 

    CGContextSaveGState(ctx); 
    NSLog(@"handleCenterA.x %f",handleCenterA.x); 
    NSLog(@" handleCenterA.y %f", handleCenterA.y); 
    [[UIColor colorWithWhite:1.0 alpha:1.0]set]; 
    UIImage *myImage = [UIImage imageNamed:@"clock-marker.png"]; 

    //this will give me the result of image 1 
    [myImage drawInRect:CGRectMake(handleCenterA.x-35, handleCenterA.y-40, TB_BUTTON_WIDTH, TB_BUTTON_WIDTH)]; 

    //this will give me the result of image 2 
    [myImage drawInRect:CGRectMake(handleCenterA.x-35, handleCenterA.y-40, TB_BUTTON_WIDTH, TB_BUTTON_WIDTH)]; 

    CGContextRestoreGState(ctx); 
} 

#pragma mark - Math - 

/** Move the Handle **/ 
-(void)movehandle:(CGPoint)lastPoint{ 

    //Get the center 
    CGPoint centerPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); 

    //Calculate the direction from a center point and a arbitrary position. 
    //float currentAngle = AngleFromNorth(centerPoint, lastPoint, NO); 
    //int angleInt = floor(currentAngle); 


    //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; 
    } 
    //Redraw 
    [self setNeedsDisplay]; 
} 

- (CGPoint)centerPointFromAngel:(int)angleInt { 
    CGPoint point = [self pointFromAngle:angleInt]; 
    point.x += TB_BUTTON_WIDTH/2; 
    point.y += TB_BUTTON_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)); 
} 

/** Given the angle, get the point position on circumference **/ 
-(CGPoint)pointFromAngle:(int)angleInt{ 

    //Circle center 
    CGPoint centerPoint = CGPointMake(self.frame.size.width/2 - (TB_BUTTON_WIDTH/2), self.frame.size.height/2 - (TB_BUTTON_WIDTH/2)); 

    //The point position on the circumference 
    CGPoint result; 
    result.y = round(centerPoint.y + radius * sin(ToRad(-angleInt))) ; 
    result.x = round(centerPoint.x + radius * cos(ToRad(-angleInt)));  
    return result; 
} 

//Sourcecode from Apple example clockControl 
//Calculate the direction in degrees from a center point to an arbitrary position. 
static inline float AngleFromNorth(CGPoint p1, CGPoint p2, BOOL flipped) { 
    CGPoint v = CGPointMake(p2.x-p1.x,p2.y-p1.y); 
    float vmag = sqrt(SQR(v.x) + SQR(v.y)), result = 0; 
    v.x /= vmag; 
    v.y /= vmag; 
    double radians = atan2(v.y,v.x); 
    result = ToDeg(radians); 
    return (result >=0 ? result : result + 360.0); 
} 
+0

Объясните, что вы хотите, потому что «окружность узлов в центре круга» тарабарщина для меня. –

+0

@ смысл-дела, я упомянул из этого урока http://www.thinkandbuild.it/how-to-build-a-custom-control-in-ios/ Функция вычисляет и рисует узел между центр круга. я не знаю, как вычислить, чтобы он приблизился к внешнему кругу. – Desmond

ответ

0

окончательно решил.

/** С учетом угла, получаем положение точки на окружности **/

-(CGPoint)pointFromAngle:(int)angleInt{ 

    //Circle center//TB_BUTTON_WIDTH 
    CGPoint centerPoint = CGPointMake(self.frame.size.width/2 - (TB_BUTTON_WIDTH/2), self.frame.size.height/2 - (TB_BUTTON_WIDTH/2)); 

    //The point position on the circumference radius 
    CGPoint result; 
    result.y = round(centerPoint.y + 120 * sin(ToRad(-angleInt))) ; 
    result.x = round(centerPoint.x + 120 * cos(ToRad(-angleInt))); 

     NSLog(@"centerPoint %@",NSStringFromCGPoint(centerPoint)); 
     NSLog(@"result %@",NSStringFromCGPoint(result)); 
    return result; 
} 
Смежные вопросы