Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 画圆的一部分_Iphone_Objective C_Cocoa Touch - Fatal编程技术网

Iphone 画圆的一部分

Iphone 画圆的一部分,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,对于一个iPhone应用程序,我想画一个圆圈,这个圆圈只代表x的填充百分比 大概是这样的: 我在计算半径、度数或弧度方面没有问题,这没有问题。此外,绘制圆已经完成。但是我如何让iphonesdk绘制填充的部分呢 我可以画一个这么大的矩形,但不能画圆的一部分 我只想在一个正常的背景下画出来 希望有人能给我一些建议。使用CGContext的arc函数: CGContextAddArc(context, centerX, centerY

对于一个iPhone应用程序,我想画一个圆圈,这个圆圈只代表x的填充百分比

大概是这样的:

我在计算半径、度数或弧度方面没有问题,这没有问题。此外,绘制圆已经完成。但是我如何让iphonesdk绘制填充的部分呢

我可以画一个这么大的矩形,但不能画圆的一部分

我只想在一个正常的背景下画出来


希望有人能给我一些建议。

使用
CGContext
的arc函数:

CGContextAddArc(context,
                centerX,
                centerY,
                radius,
                startAngleRadians,
                endAngleRadians,
                clockwise ? 1 : 0);     
请参阅文档。

尝试以下操作:

CGContextMoveToPoint(the center point)
CGContextAddLineToPoint(the starting point of the fill path on the circumference)
CGContextAddArcToPoint(the ending point of the fill path on the circumference)
CGContextAddLineToPoint(the center point)
CGContextFillPath

我实现了一个pie进度视图,它看起来与您正在做的类似。它是开源的。希望源代码能有所帮助


许多人已经向您展示了如何在核心图形中实现这一点,但也可以通过核心动画来实现,这大大增加了可以轻松设置饼图形状百分比动画的功能

下面的代码将创建环和部分填充的层(即使您说您已经可以绘制环),因为使用相同的方法绘制环和饼图形状很好

如果设置pieShape层的or属性的动画,则将设置百分比动画。与所有核心动画代码一样,您需要将QuartzCore.framework添加到项目中,并在代码中包含

// Create a white ring that fills the entire frame and is 2 points wide.
// Its frame is inset 1 point to fit for the 2 point stroke width
CGFloat radius = MIN(self.frame.size.width,self.frame.size.height)/2;
CGFloat inset  = 1;
CAShapeLayer *ring = [CAShapeLayer layer];
ring.path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(self.bounds, inset, inset) 
                                       cornerRadius:radius-inset].CGPath;

ring.fillColor   = [UIColor clearColor].CGColor;
ring.strokeColor = [UIColor whiteColor].CGColor;
ring.lineWidth   = 2;

// Create a white pie-chart-like shape inside the white ring (above).
// The outside of the shape should be inside the ring, therefore the
// frame needs to be inset radius/2 (for its outside to be on 
// the outside of the ring) + 2 (to be 2 points in).
CAShapeLayer *pieShape = [CAShapeLayer layer];
inset = radius/2 + 2; // The inset is updated here
pieShape.path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(self.bounds, inset, inset)
                                        cornerRadius:radius-inset].CGPath;
pieShape.fillColor   = [UIColor clearColor].CGColor;
pieShape.strokeColor = [UIColor whiteColor].CGColor;
pieShape.lineWidth   = (radius-inset)*2;   

// Add sublayers
// NOTE: the following code is used in a UIView subclass (thus self is a view)
// If you instead chose to use this code in a view controller you should instead
// use self.view.layer to access the view of your view controller.
[self.layer addSublayer:ring];
[self.layer addSublayer:pieShape];

下面是一个完整的方法,我正在使用,这与核心图形,适应和扩大mharper的评论以上

这段代码是针对OSX Cocoa的,但是可以通过修改获取上下文的方式轻松地更改为iOS

- (void)drawPieShapedCircleWithRadius:(CGFloat)radius
                      strokeColor:(CGColorRef)strokeColor
                        fillColor:(CGColorRef)fillColor
                        lineWidth:(CGFloat)lineWidth
                   currentDegrees:(float)currentDegrees
                     startDegrees:(float)startDegrees {
    // get the context
    CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];

    // Set the color of the circle stroke and fill
    CGContextSetStrokeColorWithColor(context, strokeColor);
    CGContextSetFillColorWithColor(context, fillColor);

    // Set the line width of the circle
    CGContextSetLineWidth(context, 1);

    // Calculate the middle of the circle
    CGPoint circleCenter = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);

    // Move the bezier to the center of the circle
    CGContextMoveToPoint(context, circleCenter.x, circleCenter.y);  // move to the center point

    // Draw the arc from the start point (hardcoded as the bottom of the circle) to the center
    CGContextAddLineToPoint(context, circleCenter.x, circleCenter.y + radius);

    // Draw the arc around the circle from the start degrees point to the current degrees point
    CGContextAddArc(context, circleCenter.x , circleCenter.y, radius, [self radians:startDegrees], [self radians:startDegrees + currentDegrees], 0);

    // Draw the line back into the center of the circle
    CGContextAddLineToPoint(context, circleCenter.x, circleCenter.y);

    // Fill the circle
    CGContextFillPath(context);

    // Draw the line around the circle
    CGContextStrokePath(context);
}

在UIView中尝试此代码,例如“MyChartClass”


尊敬。

既然到目前为止还没有人使用NSBezierPath,我想我可以提供我最近用于解决同一问题的解决方案:

-(void)drawRect:(NSRect)dirtyRect
{
    double start = -10.0; //degrees
    double end = 190.0; //degrees
    NSPoint center = NSMakePoint(350, 200);
    double radius = 50;

    NSBezierPath *sector = [NSBezierPath bezierPath];
    [sector moveToPoint:center];
    [sector appendBezierPathWithArcWithCenter:center radius:radius startAngle:start endAngle:end];
    [sector lineToPoint:center];
    [sector fill];
}

电路控制器.h

#import <UIKit/UIKit.h>

@interface CircleViewController : UIViewController

@end
#import <UIKit/UIKit.h>

@interface GraphView : UIView

@end
GraphView.h

#import <UIKit/UIKit.h>

@interface CircleViewController : UIViewController

@end
#import <UIKit/UIKit.h>

@interface GraphView : UIView

@end
注意:您可以使用以下两种方法之一: “drawCircleWithCircleCenter”或“drawCircleWithCircleCenter 2”

如果只想在两个部分上拆分单元格,请使用此代码


如果您想将单元格拆分为两个以上的部分,您可以勾选以下内容:”,并勾选以“我们有6个类”开头的答案。

谢谢!这是这篇文章的最佳答案,所以+1。非常好,简洁的代码。非常感谢您将其放在GitHub上。谢谢这很容易解决。只需添加
CGContextMoveToPoint(上下文、centerX、centerY)在前面的行中。下面是此strokeStart的动画(或者您可以使用strokeEnd):
Cabasicanization*pathAnimation=[Cabasicanization animationWithKeyPath:@“strokeEnd”];pathAnimation.duration=3.0;pathAnimation.fromValue=[NSNumber numberWithFloat:0.0f];pathAnimation.toValue=[NSNumber numberWithFloat:0.3f];pathAnimation.removedOnCompletion=否;pathAnimation.fillMode=kCAFillModeForwards;[pieShape addAnimation:pathAnimation-forKey:@“StrokeEndimation”]
#import <UIKit/UIKit.h>

@interface GraphView : UIView

@end
#import "GraphView.h"

@implementation GraphView

- (void)drawRect:(CGRect)rect {

    CGPoint circleCenter = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);

    [self drawCircleWithCircleCenter:(CGPoint) circleCenter radius:80 firstColor:[UIColor blueColor].CGColor secondeColor:[UIColor redColor].CGColor lineWidth:2 startDegree:0 currentDegree:90];
    //[self drawCircleWithCircleCenter2:(CGPoint) circleCenter radius:80 firstColor:[UIColor blueColor].CGColor secondeColor:[UIColor redColor].CGColor lineWidth:2 startDegree:0 currentDegree:90];
}

- (void)drawCircleWithCircleCenter:(CGPoint) circleCenter
                            radius:(CGFloat)radius
                            firstColor:(CGColorRef)firstColor
                            secondeColor:(CGColorRef)secondeColor
                            lineWidth:(CGFloat)lineWidth
                            startDegree:(float)startDegree
                            currentDegree:(float)endDegree {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, lineWidth);

    CGContextMoveToPoint(context, circleCenter.x, circleCenter.y);

    CGContextAddArc(context, circleCenter.x , circleCenter.y, radius, [self radians:startDegree], [self radians:endDegree], 0);
    CGContextSetFillColorWithColor(context, firstColor);
    CGContextFillPath(context);

    CGContextMoveToPoint(context, circleCenter.x, circleCenter.y);

    CGContextAddArc(context, circleCenter.x, circleCenter.y, radius, [self radians:endDegree], [self radians:startDegree], 0);
    CGContextSetFillColorWithColor(context, secondeColor);
    CGContextFillPath(context);
}

- (void)drawCircleWithCircleCenter2:(CGPoint) circleCenter
                            radius:(CGFloat)radius
                        firstColor:(CGColorRef)firstColor
                      secondeColor:(CGColorRef)secondeColor
                         lineWidth:(CGFloat)lineWidth
                       startDegree:(float)startDegree
                     currentDegree:(float)endDegree {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, lineWidth);

    CGContextMoveToPoint(context, circleCenter.x, circleCenter.y);

    CGContextAddArc(context, circleCenter.x , circleCenter.y, radius, [self radians:startDegree], [self radians:endDegree], 0);
    CGContextSetFillColorWithColor(context, firstColor);
    CGContextFillPath(context);

    CGContextMoveToPoint(context, circleCenter.x, circleCenter.y);

    CGContextAddArc(context, circleCenter.x, circleCenter.y, radius, [self radians:endDegree], [self radians:startDegree], 0);
    CGContextSetStrokeColorWithColor(context, secondeColor);
    CGContextStrokePath(context);
}

-(float) radians:(double) degrees {
    return degrees * M_PI / 180;
}


@end