菜鸟教程小白 发表于 2022-12-12 22:48:49

iphone - 如何将图像动态排列成一个圆圈


                                            <p><p>如何在圆形图像中动态排列图标(UIButtons,带有 backgroundImages)。类似的东西应该像同心圆。</p>

<p>我正在使用以下代码,</p>

<pre><code>UIImageView *container = [ initWithFrame:CGRectMake(0.0, 0.0f, 299.0f, 298.0f)];
    container.image = ;
    container.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0);
    ;

    NSArray *imagesArray = [initWithObjects:,,,,, nil];
int numberOfSections = 5;
    CGFloat angleSize = 2*M_PI/numberOfSections;
for (int j = 0; j &lt; numberOfSections; ++j) {
      UIButton *sectionLabel = [ initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 128.0f)];
forState:UIControlStateNormal];
sectionLabel.layer.anchorPoint = CGPointMake(0.9f, 0.1f);
      sectionLabel.layer.position = CGPointMake(container.bounds.size.width/2.0, container.bounds.size.height/2.0); // places anchorPoint of each label directly in the center of the circle.

      sectionLabel.transform = CGAffineTransformMakeRotation(angleSize*j);
      ;
    NSLog(@&#34;section label x and y is %f ,%f&#34;,sectionLabel.frame.origin.x,sectionLabel.frame.origin.y);
    }
    ;
</code></pre>

<p>提前致谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你可以使用这段代码:</p>

<pre><code>UIButton *currentButton;
int buttonCount = 20;
int radius = 200;
float angleBetweenButtons = (2 * M_PI) / buttonCount;
float x = 0;
float y = 0;
CGAffineTransform rotationTransform;

for (int i = 0; i &lt; buttonCount; i++)
{
    // get your button from array or something
    currentButton = ...
    ...

    x = radius + cosf(i * angleBetweenButtons) * radius;
    y = radius + sinf(i * angleBetweenButtons) * radius;
    rotationTransform = CGAffineTransformIdentity;
    rotationTransform = CGAffineTransformRotate(rotationTransform, (i * angleBetweenButtons));
    currentButton.center = CGPointMake(x, y);
    currentButton.transform = rotationTransform;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 如何将图像动态排列成一个圆圈,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/12562458/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/12562458/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 如何将图像动态排列成一个圆圈