简体   繁体   English

如何以编程方式在中心图像周围围成一圈布置图像/视图

[英]How can I layout images/views programmatically in a circle AROUND a central image

I am attempting to plot 8 UIViews in a circle around a central image. 我正在尝试在中心图像周围的圆圈中绘制8个UIView。 I am able to plot the images in a circle, but my center point is not located on the central image. 我可以将图像绘制成一个圆,但是我的中心点不在中央图像上。

I have successfully plotted 8 UIViews (will be images) programmatically in a circle using a shared center/origin point. 我已经使用共享的中心/原点成功地以编程方式绘制了8个UIView(将是图像)。 From center.x and center.y I am adding the x and y components for each particular image using trig functions. 在center.x和center.y中,我正在使用Trig函数为每个特定图像添加x和y分量。

let radiusX = center.x + (radius * cos(radians)) . 让radiusX = center.x +(radius * cos(radians))。 let radiusY = center.y + (radius * sin(radians)) 设radiusY = center.y +(半径* sin(弧度))

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var centerImage: UIImageView!{
        didSet{
            centerImage.clipsToBounds = true
            centerImage.layer.cornerRadius = 
            centerImage.frame.width/2
        }
    }
    @IBOutlet weak var containerView: UIView!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, 
        typically from a nib.

    }

    override func viewDidLayoutSubviews() {
        initSmallPhotos()
    }

    func initSmallPhotos() {

        var radians : CGFloat = 0
        let sideOfPic : CGFloat = 65.7
        let radius : CGFloat = containerView.frame.width > 
            containerView.frame.height ? 
            containerView.frame.height / 2 - 
            sideOfPic : containerView.frame.width / 2 - sideOfPic

        let x = containerView.center.x
        let y = containerView.center.y

        let center = CGPoint(x: x, y: y)

        print(center)


        for _ in 0...7 {


            let radiusX = center.x + (radius * cos(radians))
            let radiusY = center.y + (radius * sin(radians))

            let view = UIView()
            view.backgroundColor = UIColor.red
            view.frame.size = CGSize(width: sideOfPic, height: 
                 sideOfPic)
            view.center = CGPoint(x: radiusX, y: radiusY)
            view.clipsToBounds = true
            view.layer.cornerRadius = sideOfPic / 2
            view.layer.masksToBounds = true
            view.layer.borderColor = UIColor.white.cgColor
            view.layer.borderWidth = 2
            view.contentMode = .scaleAspectFit

            containerView.addSubview(view)

            radians += (CGFloat.pi / 4)
            print(radians)

        }
    }

}

The issue is what to use for my center points. 问题是我的中心点用什么。 I am able to center the 8 images horizontally by using my center.x = containerView.center.x, but when I use center.y = containerView.center.y, the images are set too low. 我可以使用我的center.x = containerView.center.x将8个图像水平居中,但是当我使用center.y = containerView.center.y时,图像设置得太低。 I also tried center.y = containerView.frame.origin.y and that set the 8 images too high on the screen. 我还尝试了center.y = containerView.frame.origin.y,这将8个图像在屏幕上设置得过高。

trying to use center points from my centerImage gave me really strange behavior, even though the image is constrained to the center of the container view. 即使图像被限制在容器视图的中心,尝试使用centerImage的中心点也给我带来了非常奇怪的行为。

Any help would be appreciated. 任何帮助,将不胜感激。

Use bounds.midX vs center.x AND bounds.midY vs. center.y.. Check the lines where you set x and y. 使用bounds.midX vs center.x和bounds.midY vs center.y.。检查设置x和y的行。 Using center is giving you the position of the containerView inside of its super view not the center of containerView. 使用center可以使containerView在其超级视图内部的位置而不是containerView的中心。 Hope that makes sense. 希望有道理。

import Foundation
import UIKit
import PlaygroundSupport
class ViewController:UIViewController{
    var check = true
    var containerView = UIView(frame: CGRect(x: 30, y: 30, width: 400, height: 300))
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .blue
        view.addSubview(containerView)

        initSmallPhotos()

    }

    func initSmallPhotos() {

        var radians : CGFloat = 0
        let sideOfPic : CGFloat = 65.7
        let radius : CGFloat = containerView.frame.width >
            containerView.frame.height ?
                containerView.frame.height / 2 -
                sideOfPic : containerView.frame.width / 2 - sideOfPic

        let x = containerView.bounds.midX
        let y = containerView.bounds.midY

        let center = CGPoint(x: x, y: y)

        print(center)


        for _ in 0...7 {


            let radiusX = center.x + (radius * cos(radians))
            let radiusY = center.y + (radius * sin(radians))

            let view = UIView()
            view.backgroundColor = UIColor.red
            view.frame.size = CGSize(width: sideOfPic, height:
                sideOfPic)
            view.center = CGPoint(x: radiusX, y: radiusY)
            view.clipsToBounds = true
            view.layer.cornerRadius = sideOfPic / 2
            view.layer.masksToBounds = true
            view.layer.borderColor = UIColor.white.cgColor
            view.layer.borderWidth = 2
            view.contentMode = .scaleAspectFit

            containerView.addSubview(view)

            radians += (CGFloat.pi / 4)
            print(radians)

        }
    }
}


let viewController = ViewController()
PlaygroundPage.current.liveView = viewController
PlaygroundPage.current.needsIndefiniteExecution

例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM