ios - View 内的多个 subview 和自动调整大小
<p><p>我正在尝试使用以下代码创建一个代表 Logo 的组件:</p>
<pre><code>- (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,
CGAffineTransformMakeScale(self.frame.size.width / 213.0,
self.frame.size.height / 213.0));
EllipseView * v = [ initWithFrame:
CGRectOffset(rect, self.frame.size.width/2.0, self.frame.size.height/2)];
v.tag = 90000 + i;
v.tintColor = self.tintColor;
;
}
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*);
v.transform = CGAffineTransformMakeScale(ratio, ratio);
}
}
</code></pre>
<p>我很难使用 <code>CGAffineTransform</code> 操作。关于如何处理 View 中的多个 subview 并将它们作为一个组调整大小的任何提示?</p>
<p> <a href="/image/FwwBT.gif" rel="noreferrer noopener nofollow"><img src="/image/FwwBT.gif" alt="enter image description here"/></a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>根据<code>UIView</code>引用:</p>
<blockquote>
<p><code>transform</code> Property </p>
<p>Specifies the transform applied to the receiver,
relative to the center of its bounds.</p>
</blockquote>
<p>注意“边界的中心”。转换只是缩放 <code>EllipseView</code> 的“内容”。由于 View 没有在 <code>layoutSubviews</code> 中移动,因此它们保持在原来的位置,只有点被缩放。</p>
<p>你必须使用转换吗?</p>
<p>您可以像在 <code>createSubviews</code> 中那样更改 View 的框架。</p>
<p>由于您已经有了坐标,您可以创建一个 <code>CAShapeLayer</code>,根据您的 <code>CGRect</code> 数组构建一个带有椭圆的 <code>UIBezierPath</code>,然后添加图层作为主视图的子图层,然后对其进行缩放。</p></p>
<p style="font-size: 20px;">关于ios -View 内的多个 subview 和自动调整大小,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32791921/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32791921/
</a>
</p>
页:
[1]