【问题标题】:Draw line between two moveable circles in objective-c (iPad)在objective-c(iPad)中的两个可移动圆圈之间画线
【发布时间】:2013-01-10 18:04:29
【问题描述】:

我是 Objective-c 的新手,正在尝试在 Objective-c 中的可移动圆圈之间画一条线。我已经有生成圆圈的代码。这是我想在我的应用中创建的图像。

http://images.sciencedaily.com/2004/04/040407083832.jpg

这是我的代码。

CircleViewController.m

   - (void)viewDidLoad
   {
     [super viewDidLoad];
     for (int i=0; i<5; i++) {

    CGRect circleFrame = CGRectMake(arc4random() % 500, arc4random() % 500, (arc4random() % 200)+50 , (arc4random() % 200)+50);
    CircleView *cirleView = [[CircleView alloc] initWithFrame: circleFrame];
    cirleView.backgroundColor = [UIColor clearColor];

    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
    UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
    cirleView.circleColor = color;
    [self.view addSubview:cirleView];
 }

}

CircleView.m

   -(void) drawCircle:(CGPoint)p withRadius:(CGFloat)radius inContext:(CGContextRef)contex
   {
       UIGraphicsPushContext(contex);
       CGContextBeginPath(contex);
       CGContextAddArc(contex, p.x, p.y, radius, 0, 2*M_PI, YES);
       CGContextSetLineWidth(contex, 2.0);
       CGContextAddLineToPoint(contex, p.x, p.y);
       CGContextDrawPath(contex, kCGPathFillStroke);
       UIGraphicsPopContext(); 
   }

   - (void)drawRect:(CGRect)rect
   {

        CGFloat size = self.bounds.size.width/2;
        if(self.bounds.size.height < self.bounds.size.width) size = self.bounds.size.height / 2;

        size *= 0.90;
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, 5.0);

        [_circleColor setStroke];
        [_circleColor setFill];    
        CGPoint point1;
        point1.x = self.bounds.origin.x + self.bounds.size.width/2;
        point1.y = self.bounds.origin.y + self.bounds.size.height/2;

        [self drawCircle:point1 withRadius:size inContext:context];

        UITapGestureRecognizer *singleFingerTap =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                        action:@selector(handleSingleTap:)];
        [self addGestureRecognizer:singleFingerTap];
   }

感谢您的帮助。

【问题讨论】:

    标签: ios objective-c ipad uiview


    【解决方案1】:

    画一条线:

    -(void) drawLine (CGRect circleViewRect1, CGRect circleViewRect2)
    {
        CGContextRef context   = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    
        //line width
        CGContextSetLineWidth(context, 1.0);
    
        CGContextMoveToPoint(context, circleViewRect1.center.x + circleViewRect1.bounds.width/2,circleViewRect1.center.y + circleViewRect1.bounds.height/2); //start from first circle radius
    
        CGContextAddLineToPoint(context,  circleViewRect2.center.x + circleViewRect2.bounds.width/2,circleViewRect2.center.y + circleViewRect2.bounds.height/2); //draw to this point
    
        // and now draw the Path!
        CGContextStrokePath(context);
    }
    

    注意:这仅在两个圆的中心位于水平线上时才有用。此外,假设 circleViewRect1 位于 circleViewRect2 的左侧。您必须弄清楚其他用例的值,即它们是否位于不同的角度等。

    【讨论】:

    • 感谢您抽出时间给我一个想法。
    猜你喜欢
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 2021-01-22
    • 1970-01-01
    相关资源
    最近更新 更多