无法缩放形状以匹配UIView的尺寸

时间:2017-09-30 03:57:40

标签: ios objective-c iphone uiview

我创建了一个自定义视图,可以绘制不同边的形状。该视图作为子视图添加到主视图中,如下所示。形状具有不同的尺寸。

Multiple UIView shapes in a subview

Individual Subviews

我的源代码如下所示

-(instancetype) initWithSides:(NSUInteger) sides andFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            [self setBackgroundColor:[UIColor clearColor]];
            self.sides = sides;
            self.radius =  CGRectGetWidth(self.frame) * 1.5 / sides);
        }
        return self;
}

    -(void) drawRect:(CGRect) frame {
// Sides is passed as constructor argument. 4 sides means a quadrilateral etc.
// Get CGPAthRef instance

        CGFloat angle = DEGREES_TO_RADIANS(360 / ((CGFloat) self.sides));
    int count = 0;
    CGFloat xCoord = CGRectGetMidX(self.frame);
    CGFloat yCoord = CGRectGetMidY(self.frame);
    while (count < self.sides) {
        CGFloat xPosition =
            xCoord + self.radius * cos(angle * ((CGFloat) count));
        CGFloat yPosition =
            yCoord + self.radius * sin(angle * ((CGFloat) count));

        [self.points addObject:[NSValue valueWithCGPoint:CGPointMake(xPosition, yPosition)]];

        count ++;
    }

    NSValue* v = [self.points firstObject];
    CGPoint first = v.CGPointValue;

    CGMutablePathRef path = CGPathCreateMutable();
     CGPathMoveToPoint(path, nil, first.x, first.y);

    for (int ix = 1; ix < [self.points count]; ix++) {
        NSValue* pValue = [self.points objectAtIndex:ix];
        CGPoint p = pValue.CGPointValue;
        CGPathAddLineToPoint(path, nil, p.x, p.y);
    }

    CGPathCloseSubpath(path);


   CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddPath(context, path);
    [self colourView:context withPath:path];
  }

-(void) colourView:(CGContextRef) context withPath:(CGPathRef) ref {
    NSUInteger num = arc4random_uniform(8) + 1;
    UIColor* color = nil;
    switch (num) {
        case 1:
            color = [UIColor redColor];
            break;
        case 2:
            color = [UIColor greenColor];
            break;
        case 3:
            color = [UIColor yellowColor];
            break;
        case 4:
            color = [UIColor blueColor];
            break;
        case 5:
            color = [UIColor orangeColor];
            break;
        case 6:
            color = [UIColor brownColor];
            break;
        case 7:
            color = [UIColor purpleColor];
            break;
        case 8:
            color = [UIColor blackColor];
            break;
        default:
            break;
    }

    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillPath(context);
}

这构成了一个单一的形状。这就是我绘制其余视图的方式。

-(void) initDrawView {
    NSUInteger width = CGRectGetWidth(self.view.bounds) / 8;
    NSUInteger height = (CGRectGetHeight(self.view.frame))/ 8;
    UITapGestureRecognizer *singleFingerTap =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleSingleTap:)];
    for (int i = 0 ; i < 8; i++) {
        CGFloat yCoord = i * height + i * 15;
        for (int j = 0; j < 8; j ++) {
            int side = 3 + arc4random() % 8;
            CGFloat xCoord = j * width;

            SimpleRegularPolygonView* view =
                [[SimpleRegularPolygonView alloc] initWithSides:side andFrame:CGRectMake(xCoord, yCoord, width, height)];
            [view sizeToFit];
            view.viewEffectsDelegate = self;
            [view setTag: (8 * i + j)];
            [self.view addGestureRecognizer:singleFingerTap];
            [self.view addSubview:view];
        }
    }
}

1)我不知道如何让它们具有相同的尺寸。我怎样才能做到这一点? (第一张图片)

2)图像不能缩放到UIView(第二张图像)的大小。

1 个答案:

答案 0 :(得分:1)

您当前的代码使半径与边数成反比:

self.radius =  CGRectGetWidth(self.frame) * 1.5 / sides;

所以侧面越多,图像越小。快速解决方法是将半径设为框架宽度的一半:

self.radius =  CGRectGetWidth(self.frame) /2;

这意味着具有偶数个边的形状填充框架宽度。但那些边数奇数的人似乎在左边有空间。如果要调整宽度,则还需要移动形状的“中心”。对于奇数边,半径必须是:

self.radius =  CGRectGetWidth(self.frame) /(1 + cos(angle / 2));

和xCoord需要:

CGFloat xCoord = CGRectGetMinX(self.frame) + self.radius * cos(angle/2);
相关问题