我正在尝试在一个中心图像周围的圆周上绘制8个UIView。我可以将图像绘制成一个圆,但是我的中心点不在中央图像上。
我已经使用共享的中心/原点成功地以编程方式绘制了8个UIView(将为图像)。从center.x和center.y开始,我要使用trig函数为每个特定图像添加x和y分量。
let radiusX = center.x +(radius * cos(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)
}
}
}
问题是我的中心点用什么。我可以使用我的center.x = containerView.center.x将8个图像水平居中,但是当我使用center.y = containerView.center.y时,图像设置得太低。我还尝试了center.y = containerView.frame.origin.y,这将8张图像在屏幕上设置得过高。
尝试使用我的centerImage的中心点给我一个非常奇怪的行为,即使图像被限制在容器视图的中心。
任何帮助将不胜感激。
答案 0 :(得分:0)
使用bounds.midX与center.x以及bounds.midY与center.y ..检查设置x和y的行。使用center可以使containerView在其超级视图内部的位置而不是containerView的中心。希望有道理。
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
https://firebase.google.com/docs/database/admin/save-data#section-push