【问题标题】:how to shift the slider image knot outside如何将滑块图像结移到外面
【发布时间】:2013-04-25 09:33:42
【问题描述】:

我很难理解如何计算结的周长到圆心的数学。希望大家多多指教。

当前计算将结点 img 设置在饼图的中间,希望像 img 2 一样将其移到更靠近外圈的位置

感谢您的观看和评论,感谢任何cmets。

我想怎么样。

/** 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);
}

【问题讨论】:

  • 解释你想要什么,因为“结的圆周到圆心”对我来说是胡言乱语。
  • @meaning-matters,我在本教程中提到了thinkandbuild.it/how-to-build-a-custom-control-in-ios 函数计算并在圆心之间绘制结。我不知道如何计算,所以它会更接近外圈。

标签: iphone ios xcode core-graphics


【解决方案1】:

终于解决了。

/** 给定角度,得到圆周上的点位置**/

-(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;
}

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 2013-06-08
    • 2019-05-26
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多