简体   繁体   English

在矩形上绘制圆圈

[英]Draw circle on rectangle

I have UIView class and in method I want to draw first rectangles and sometimes circle 我有UIView类,在方法中我想绘制第一个矩形,有时是圆形

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    if ([WhatToDraw isEqual:@"Fields"]) {
        [self DrawField:context];
            }
    if ([WhatToDraw isEqual:@"Ball"]) {
        [self DrawBall:context x:20 y:20];
    }

}

-(void)DrawBall:(CGContextRef)context x:(float) x y:(float) y
{
    UIGraphicsPushContext(context);
    CGRect  rect = CGRectMake(x, y, 25, 25);
    CGContextClearRect(context, rect);
    CGContextFillEllipseInRect(context, rect);
}

-(void)DrawField:(CGContextRef)context 
{

    columns = 6;
    float offset = 5;
    float boardWidth = self.frame.size.width;
    float allOffset = (columns + 2) * offset;
    float currentX = 10;
    float currentWidth = (boardWidth - allOffset) / columns;
    float currentHeight = currentWidth;
    self.fieldsArray = [[NSMutableArray alloc] init];
    //create a new dynamic button board
    for (int columnIndex = 0; columnIndex<columns; columnIndex++) {
        float currentY = offset;
        for (int rowIndex=0; rowIndex<columns; rowIndex++) {
            UIGraphicsPushContext(context);
            //create new field


            CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
            CGContextBeginPath(context);
            CGRect rect = CGRectMake(currentX, currentY, currentWidth, currentHeight);
            CGContextAddRect(context, rect);
            CGContextFillPath(context);

            currentY = currentY + offset + currentHeight;
        }
        currentX = currentX + offset + currentWidth;
    }  
}

I also have method changing what to draw 我也有改变绘画方法的方法

-(void)Draw:(NSString*)Thing
{
    self.WhatToDraw = Thing;
    [self setNeedsDisplay];
}

Drawing rectangles (Fields) is ok, but when I click button to draw circle all rectangles disappear and only circle was drawn. 绘制矩形(字段)是可以的,但是当我单击按钮绘制圆形时,所有矩形都消失,只绘制圆形。 How can I draw circle on existing rectangle ? 如何在现有矩形上绘制圆圈?

The Problem 问题

Your problem is that when a UIView redraws a region as marked by setNeedsDisplay or setNeedsDisplayInRect it will completely clear that region before executing your drawing code. 您的问题是,当UIView重绘由setNeedsDisplaysetNeedsDisplayInRect标记的区域时,它将在执行绘图代码之前完全清除该区域。 This means that unless you draw both the rectangles and circle in a single drawing operation within drawRect you will never see the two both drawn in the area you choose to redraw, whether it be the entire view bounds with setNeedsDisplay or a specific area with setNeedsDisplayInRect . 这意味着,除非你在一个单一的绘图操作中绘制两个矩形和圆形drawRect你永远不会看到两人双双在您选择重绘区域抽出的,无论是整个视图边界与setNeedsDisplay或特定区域setNeedsDisplayInRect

The Solutions 解决方案

There's no reason why you can't draw both the rectangles and circle each time within drawRect and optimise the performance of the drawing by only redrawing the regions necessary with setNeedsDisplayInRect . 没有理由不能在drawRect每次都不能绘制矩形和圆形,只通过重绘setNeedsDisplayInRect所需的区域来优化绘图的性能。

Alternatively you could break up the content using CALayers and have the rectangles in one layer and the circle in another. 或者,您可以使用CALayers分解内容,并将矩形放在一个图层中,将圆圈放在另一个图层中。 This would allow you to leverage the animation capabilities of Core Animation . 这将允许您利用Core Animation的动画功能。 Core animation provides a simple and effective way to manipulate onscreen layers with implicit animations such as moving, resizing, changing colour etc. 核心动画提供了一种简单有效的方法,可以使用隐式动画操作屏幕图层,例如移动,调整大小,更改颜色等。

我的猜测,你的DrawBall方法中的CGContextClearRect调用是矩形消失的责任...来自Apple文档: 如果提供的上下文是窗口或位图上下文,Quartz会有效地清除矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM