OGeek|极客世界-中国程序员成长平台

标题: ios - View 内的多个 subview 和自动调整大小 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:09
标题: ios - View 内的多个 subview 和自动调整大小

我正在尝试使用以下代码创建一个代表 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



Best Answer-推荐答案


根据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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4