如何在 iOS 设备上的 swift 中以编程方式在 UIImageView 后面显示彩色磁盘

how to programmatically show a colored disk behind a UIImageView, in swift on an iOS device

我有一个以编程方式显示的图标。
如何在同一屏幕位置的图标后面显示实心彩色圆盘?

图标不透明。

我有时会以编程方式更改屏幕位置以及磁盘的直径和颜色。

下面是我现在如何以编程方式显示图标................................

DispatchQueue.main.async
{

       ViewController.wrist_band_UIImageView.frame = CGRect(   x: screen_position_x, 
                                                               y: screen_position_y,
                                                               width: App_class.screen_width,
                                                               height: App_class.screen_height)
}

更新 #1 以下 D.Mika...

更新 #2 以下 D.Mika...

import UIKit
            import Foundation


class ViewController: UIViewController 
{
    static var circleView: UIView!

    static let wrist_band_UIImageView: UIImageView = {
       let theImageView = UIImageView()
       theImageView.image = UIImage( systemName: "applewatch" )
       theImageView.translatesAutoresizingMaskIntoConstraints = false 
       return theImageView
    }()

    override func viewDidLoad()     
    {
            super.viewDidLoad()

            view.addSubview( ViewController.wrist_band_UIImageView )
        // Init disk image:
        ViewController.circleView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
        ViewController.circleView.backgroundColor = .red
        ViewController.circleView.layer.cornerRadius = view.bounds.size.height / 2.0
        ViewController.circleView.clipsToBounds = true
            // Add view to view hierarchy below image view
        ViewController.circleView.insertSubview(view, belowSubview: ViewController.wrist_band_UIImageView)


        App_class.display_single_wearable()
    }
 }

 class App_class
 {
    static var is_communication_established = false
    static var total_packets = 0

    static func display_single_wearable()
    {
                ViewController.wrist_band_UIImageView.frame = CGRect(x: 0, y: 0,
                                                                      width: 100, height: 100)

                ViewController.circleView.frame = CGRect(   x: 0,
                                             y: 0,
                                             width: 100,
                                             height: 100)
                ViewController.circleView.layer.cornerRadius = 100
    }

    static func process_location_xy(text_location_xyz: String)
    {}
} // App_class

您可以使用以下视图显示彩色圆圈:

circleView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
circleView.backgroundColor = .red
circleView.layer.cornerRadius = view.bounds.size.height / 2.0
circleView.clipsToBounds = true
// Add view to view hierarchy below image view
view.insertSubview(circleView, belowSubview: wrist_band_UIImageView)

您可以通过应用新框架来更改它的位置和大小。但是你必须相应地改变图层的cornerRadius。

这是 playground 中的一个简单示例:

编辑: 我修改了示例代码以将视图添加到视图层次结构中。 但是,当您修改图像视图的框架时,您仍然需要调整圆圈的框架。 (以及相应的 cornerRadius)。例如

DispatchQueue.main.async
{
    
    ViewController.wrist_band_UIImageView.frame = CGRect(   x: screen_position_x,
                                                            y: screen_position_y,
                                                            width: App_class.screen_width,
                                                            height: App_class.screen_height)
    circleView.frame = CGRect(   x: screen_position_x,
                                 y: screen_position_y,
                                 width: App_class.screen_width,
                                 height: App_class.screen_height)
    circleView.layer.cornerRadius = App_class.screen_width
}

并且您需要向 viewController 添加一个变量来保存对圆视图的引用,例如:

var circleView: UIView!

以下答案有效。

以上 d.mika 的答案无效。另外,他在侮辱人。 ;-)

// Show red circle
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: 200, y: 400), radius: CGFloat(40), startAngle: CGFloat(0), endAngle: CGFloat(Double.pi * 2), clockwise: true)
    
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circlePath.cgPath
    
    // Change the fill color
    shapeLayer.fillColor = UIColor.red.cgColor
    // You can change the stroke color
    shapeLayer.strokeColor = UIColor.red.cgColor
    // You can change the line width
    shapeLayer.lineWidth = 3.0
    
    view.layer.addSublayer(shapeLayer)