在Xcode中绘制一个矩形圆

时间:2015-06-29 04:53:41

标签: ios objective-c

我试过这段代码画一个矩形的圆圈

incNumberDefault=0;
for (int i = 0; i < maxNumber; i++) {
   incNumberDefault = incNumberDefault +5;


   UIView *myView  = [[UIView alloc] initWithFrame:CGRectMake(incNumberDefault, 100, 3, 15)];
   myView.center = CGPointMake(130,130);
   myView.transform = CGAffineTransformMakeRotation(DegreesToRadians(incNumberDefault));
   myView.backgroundColor = [UIColor lightGrayColor];
   [self.speedDisplay addSubview:myView];
}

输出似乎是圆圈,因为我需要在圆圈中间透明或空间。

3 个答案:

答案 0 :(得分:2)

您可以将UIView cornerRadius设置为UIView宽度或高度的一半,如

myView.layer.cornerRadius=myview.frame.size.width / 2;

myView.layer.cornerRadius=myview.frame.size.height / 2

为此你必须给你的UIView提供相同的宽度和高度。

答案 1 :(得分:0)

试试这个:

// initialize the view with same height and width for perfect circle.
   UIView *myView  = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 15, 15)];

// round the corner of view with required radius. 
    myView.layer.cornerRadius = myView.frame.size.width/2; // height can also be used 

// circle with space in middle.
    myView.backgroundColor = [UIColor clearColor];
    myView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    myView.layer.borderWidth = 2.0f;
   [self.speedDisplay addSubview:myView];

希望它能解决你的问题。

答案 2 :(得分:0)

感谢您的回答,但我想在一定距离内开发一个虚线圆圈,我已经厌倦了这段代码:

incNumberDefault=0;
    for (int i = 0; i < maxNumber; i++) {
     UIView *myView  = [[UIView alloc] initWithFrame:CGRectMake(100+60 * sin(incNumberDefault), 100+60*cos(incNumberDefault), 3, 15)];
     //myView.transform = CGAffineTransformMakeRotation(DegreesToRadians(incNumberDefault));
     myView.backgroundColor = [UIColor lightGrayColor];
     [self.speedDisplay addSubview:myView];
      incNumberDefault = incNumberDefault +9;

   }

但它会在圆圈中生成随机矩形。 我想按顺序。

谢谢。