Xcode - 围绕中心绘制圆圈 [英] Xcode - Draw Circle Around Centre

查看:247
本文介绍了Xcode - 围绕中心绘制圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何围绕从控件的位置取得的一个点画一个圆圈。

I was wondering how to draw a circle around a point taken from a control's positioning.

这不符合预期

    CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, 2.0);
CGContextSetRGBFillColor(contextRef, 0, 0, 1.0, 1.0);
CGContextSetRGBStrokeColor(contextRef, 0, 0, 1.0, 1.0);
CGRect circlePoint = (CGRectMake(self.frame.size.width / 2, self.frame.size.height / 2, 10.0, 10.0));

CGContextFillEllipseInRect(contextRef, circlePoint);


推荐答案

我认为使用UIBezierPath绘制圆圈非常容易。您需要做的就是创建UIView子类并创建UIBezierPath。我使用UIBezierPath创建了一个示例:

I think its pretty easy to draw a circle using UIBezierPath. All you need to do is subclass UIView and create UIBezierPath. I have created a example using UIBezierPath :

创建一个名为CirecleView的UIView的子类。添加以下代码:

Create a subclass of UIView called CirecleView. Add following code :

#import "CircleView.h"

@implementation CircleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGFloat rectX = self.frame.size.width / 2;
    CGFloat rectY = self.frame.size.height / 2;
    CGFloat width = 100;
    CGFloat height = 100;
    CGFloat centerX = rectX - width/2;
    CGFloat centerY = rectY - height/2;


    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(centerX, centerY, width, height)];

    [[UIColor blackColor] set];
    [bezierPath stroke];
}

@end

将控制器的视图类更改为CircleView。请参见下图。

Change your controller's view class to CircleView. Please see below picture.

多数民众赞成。运行代码以显示控制器的视图。以下是输出:

Thats it. Run your code to present your controller's view. Following is the output :

您可以从这里

这篇关于Xcode - 围绕中心绘制圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆