gpt4 book ai didi

ios - View 内的多个 subview 和自动调整大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:12:17 24 4
gpt4 key购买 nike

我正在尝试使用以下代码创建一个代表 Logo 的组件:

- (void) createSubviews
{
CGRect path[] = {
CGRectMake(62.734375,-21.675000,18.900000,18.900000),
CGRectMake(29.784375,-31.725000,27.400000,27.300000),
CGRectMake(2.534375,-81.775000,18.900000,18.900000),
CGRectMake(4.384375,-57.225000,27.400000,27.300000),
CGRectMake(2.784375,62.875000,18.900000,18.900000),
CGRectMake(4.334375,29.925000,27.400000,27.300000),
CGRectMake(62.734375,2.525000,18.900000,18.900000),
CGRectMake(29.784375,4.475000,27.400000,27.300000),
CGRectMake(-21.665625,-81.775000,18.900000,18.900000),
CGRectMake(-31.765625,-57.225000,27.400000,27.300000),
CGRectMake(-81.615625,-21.425000,18.900000,18.900000),
CGRectMake(-57.215625,-31.775000,27.400000,27.300000),
CGRectMake(-81.615625,2.775000,18.900000,18.900000),
CGRectMake(-57.215625,4.425000,27.400000,27.300000),
CGRectMake(-21.415625,62.875000,18.900000,18.900000),
CGRectMake(-31.765625,29.925000,27.400000,27.300000)};

for (int i = 0; i < 16; i++) {
CGRect rect = CGRectApplyAffineTransform(path[i],
CGAffineTransformMakeScale(self.frame.size.width / 213.0,
self.frame.size.height / 213.0));
EllipseView * v = [[EllipseView alloc] initWithFrame:
CGRectOffset(rect, self.frame.size.width/2.0, self.frame.size.height/2)];
v.tag = 90000 + i;
v.tintColor = self.tintColor;
[self addSubview:v];
}
self.initialLenght = self.frame.size.width;
}

-(void) layoutSubviews
{
CGFloat ratio = self.frame.size.width / self.initialLenght;
for (int i = 0; i < 16; i++) {
EllipseView * v = (EllipseView*)[self viewWithTag:90000+i];
v.transform = CGAffineTransformMakeScale(ratio, ratio);
}
}

我在使用 CGAffineTransform 操作时遇到了困难。关于如何处理 View 内的多个 subview 并保持它们作为一个组调整大小的任何提示?

enter image description here

最佳答案

根据UIView引用:

transform Property

Specifies the transform applied to the receiver, relative to the center of its bounds.

注意“边界的中心”。转换只是缩放 EllipseView 的“内容”。由于 View 在 layoutSubviews 中没有移动,它们保持在它们的位置并且只有点被缩放。

你必须使用转换吗?

您可以像在 createSubviews 中那样更改 View 的框架。

因为你已经有了坐标,你可以创建一个 CAShapeLayer,根据你的 CGRect 数组构建一个带椭圆的 UIBezierPath,添加层作为主视图的子层,然后对其进行缩放。

关于ios - View 内的多个 subview 和自动调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32791921/

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