6

In Paintcode 2, I have a circle inside a frame inside a canvas.

The constraints on the circle are set like this:

enter image description here

To get the circle to size up and not become an ellipse, I have to

  1. Know the expected aspect ratio
  2. Code it in Objective-C myself

Is there any way around this type of code?

-(void)drawRect:(CGRect)rect {
    if (rect.size.width > rect.size.height) {
        rect.origin.x = (rect.size.width - rect.size.height) * .5f;
        rect.size.width = rect.size.height;
    } else {
        rect.origin.y = (rect.size.height - rect.size.width) * .5f;
        rect.size.height = rect.size.width;
    }
    NSLog(@"Frame=%@", NSStringFromCGRect(rect));
    [CircleDraw drawCircleWithFrame:rect];
}
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421

1 Answers1

4
  1. Create a canvas, 150x120
  2. Create an ellipse, 10, 10, 100, 100
  3. Create a new variable called frame, type rectangle: 10, 10, 150, 100
  4. Create a new expression called smallestSide: min(frame.height, frame.width)
  5. Make an expression called position (below)
  6. Drag smallestSide to both height and width of the ellipse
  7. Drag the position to the position of the ellipse


position (Expression)
 makePoint(
    frame.x+(frame.width-smallestSide)*0.5,
    frame.y+(frame.height-smallestSide)*0.5
)


output
- (void)drawCanvas1WithFrame: (CGRect)frame
{

    //// Variable Declarations
    CGFloat smallestSide = MIN(frame.size.height, frame.size.width);
    CGPoint position = CGPointMake(frame.origin.x + (frame.size.width - smallestSide) * 0.5, frame.origin.y + (frame.size.height - smallestSide) * 0.5);

    //// Oval Drawing
    UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(position.x, position.y, smallestSide, smallestSide)];
    [UIColor.grayColor setFill];
    [ovalPath fill];
}

NOTE: I had the help of Matt Dunik at PaintCode to figure this out, but the solution is actually very straightforward.

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421