You need to enable JavaScript to run this app. Swift on iOS:全局队列更新主队列UIImageView无法显示
优惠活动
大模型
产品
解决方案
定价
更多

Swift on iOS:全局队列更新主队列UIImageView无法显示

UIImageView无法正常显示的核心问题与修复

你写的代码存在3个直接导致视图不可见的硬错误,修正后即可实现图标移动效果:

问题1:计算的视图坐标完全超出屏幕可视范围

按照你代码中的初始参数,首个位置计算结果为:

  • x坐标 = (100/2 + 0) * 10 = 500
  • y坐标 = (100 - 0) * 10 = 1000
    iOS设备的坐标原点位于屏幕左上角,以主流iPhone机型为例,屏幕最大宽度仅390、最大高度仅844。你计算出的坐标已经完全在屏幕可视区域外,自然无法看到图标。
    同时你定义的screen_offset_x、screen_offset_y两个变量全程没有参与坐标计算,属于无效冗余代码。

问题2:Auto Layout属性配置错误,手动设置的frame完全失效

你在初始化UIImageView时设置了*translatesAutoresizingMaskIntoConstraints = false*,这个配置的含义是关闭Auto Layout对手动frame的兼容,视图位置完全依赖开发者添加的NSLayoutConstraint确定。
但你全程没有给这个UIImageView添加任何布局约束,系统最终会将视图的位置和大小默认为零点,哪怕后续在主线程手动修改frame也不会生效。

问题3:循环逻辑存在死循环风险

你读到结束标记值-999时,直接将locations_index重置为0,没有设置退出逻辑,会导致全局队列上的无限循环持续阻塞,长期运行会造成内存占用过高。

可直接运行的修复代码

关键修改说明:

  • 修正坐标计算逻辑,将图标限制在屏幕可视范围内
  • 适配手动frame布局的属性配置
  • 补充正常的循环退出逻辑
import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var app_title: UILabel!

    static let wrist_band_UIImageView: UIImageView = {
       let theImageView = UIImageView()
       theImageView.image = UIImage(systemName: "applewatch")
       // 手动维护frame时必须设为true,否则Auto Layout会忽略所有手动位置设置
       theImageView.translatesAutoresizingMaskIntoConstraints = true
       // 加临时背景色方便调试定位
       theImageView.backgroundColor = .systemBlue
       return theImageView
    }()

    override func viewDidLoad()         
    {
        print("viewDidLoad...")
        super.viewDidLoad()
        view.addSubview(ViewController.wrist_band_UIImageView)
        // 初始帧先放到屏幕中心,避免异步更新前的空白
        ViewController.wrist_band_UIImageView.frame = CGRect(x: 100, y: 100, width: 60, height: 60)
        DispatchQueue.global().async{ 
            app_class.run_app()
        }
    }
 }

 class app_class
 {
    // X, Y, Z, timestamp in seconds
    static let sim_locations_track_Array: [Int]  = [     0, 0, 0,   1,
                                                            50, 1, 0,   1,
                                                            100, 2, 0,   1,
                                                            150, 3, 0,   1,
                                                            0, 0, 0,   -999 ]

    static let items_per_time = 4
    // 直接获取真实屏幕尺寸,不用硬编码的100x100
    static let screen_width = UIScreen.main.bounds.width
    static let screen_height = UIScreen.main.bounds.height
    static var screen_offset_x: CGFloat = 50
    static var screen_offset_y: CGFloat = 100
    static var iconSize: CGFloat = 60

    static func run_app()
    {
        var locations_index = 0
        let map_multiplier: CGFloat = 1.5
        // 按数组长度控制循环,避免越界
        while locations_index < sim_locations_track_Array.count / items_per_time
        {
            let board_x = CGFloat(sim_locations_track_Array[ locations_index * items_per_time ])
            let board_y = CGFloat(sim_locations_track_Array[ locations_index * items_per_time + 1 ])
            let relative_timestamp_seconds = sim_locations_track_Array[ locations_index * items_per_time + 3 ]
            
            if relative_timestamp_seconds > 0
            {
                // 将之前定义的偏移量参与计算,保证坐标在屏幕范围内
                let screen_x = board_x * map_multiplier + screen_offset_x
                let screen_y = screen_height - board_y * map_multiplier - screen_offset_y
                DispatchQueue.main.async{
                    ViewController.wrist_band_UIImageView.frame = CGRect(
                        x: screen_x,
                        y: screen_y,
                        width: iconSize, 
                        height: iconSize
                    )
                }
                sleep(UInt32(relative_timestamp_seconds))
                locations_index += 1
            } else {
                // 碰到结束标记直接跳出循环,避免无限跑
                break
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Doug Null

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.23 05:15:04