gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:41:10 25 4
gpt4 key购买 nike

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

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);
}

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

-(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 的大小(第二张图片)。

最佳答案

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

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);

关于ios - 无法缩放形状以匹配 UIView 的尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46499425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com