子类 UIImageView layoutSubviews() 与 init()

Subclass UIImageView layoutSubviews() vs. init()

我发现了一个问题,即重复调用 layoutSubviews(),我在其中创建了我的子视图(UIImageView)并附加了必要的动画,导致动画不断重复而无法停止。因此,有必要寻找另一种方法来实现子视图的创建和动画的附加。

显而易见的事情是移动 layoutSubviews() 代码

    override func layoutSubviews() {
        super.layoutSubviews()
        print("layoutSubviews()")
        spindleLeft = addSpindle(index: 1, posX: 505, posY: 350)
        spindleRight = addSpindle(index: 2, posX: 1610, posY: 350)

        spinSpindle(spindle: spindleLeft)
        spinSpindle(spindle: spindleRight)
    }

初始化

    init(cassetteType: CassetteType) {
        self.cassetteType = cassetteType
        super.init(image: UIImage(named: self.cassetteType.rawValue + "Cassette"))
        self.translatesAutoresizingMaskIntoConstraints = false
        self.isUserInteractionEnabled = true
        self.contentMode = .scaleAspectFit

        print("init(cassetteType:)")

        spindleLeft = addSpindle(index: 1, posX: 505, posY: 350)
        spindleRight = addSpindle(index: 2, posX: 1610, posY: 350)

        spinSpindle(spindle: spindleLeft)
        spinSpindle(spindle: spindleRight)
    }

但是,当我这样做时,主轴子视图不再显示。它们看起来确实已创建并且应用了动画,但它们不再可见。

为什么会这样? layoutSubviews 和 init 在这方面有何不同?

addSpindle() 代码像这样创建每个子视图:

    func addSpindle(index: Int, posX: CGFloat, posY: CGFloat) -> UIImageView {
        let cassetteBounds = self.bounds
        let cassetteWidth = cassetteBounds.width
        let cassetteHeight = cassetteBounds.height

        let cassetteImageWidth =  self.image!.size.width
        let cassetteImageHeight = self.image!.size.height

        var scale = cassetteWidth / cassetteImageWidth

        var spindleWidth = cassetteWidth
        var spindleHeight = cassetteImageHeight*scale
        var spindleX = 0 as CGFloat
        var spindleY = (cassetteHeight - spindleHeight)/2

        if spindleHeight > cassetteHeight {
            scale = cassetteHeight / cassetteImageHeight
            spindleHeight = cassetteHeight
            spindleWidth = cassetteImageWidth*scale
            spindleY = 0 as CGFloat
            spindleX = (cassetteWidth - spindleWidth)/2
        }

        let spindle = UIImageView(image: UIImage(named: self.cassetteType.rawValue + "Spindle"))
        self.addSubview(spindle)

        spindle.frame.origin.x = spindleX + (posX*scale)
        spindle.frame.origin.y = spindleY + (posY*scale)
        spindle.frame.size.width = spindle.image!.size.width*scale //299.0
        spindle.frame.size.height = spindle.image!.size.height*scale //299.0

        let gesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
        gesture.numberOfTouchesRequired = 1
        gesture.minimumPressDuration = 0

        spindle.isUserInteractionEnabled = true
        spindle.isExclusiveTouch = true
        spindle.tag = index
        spindle.addGestureRecognizer(gesture)

        return spindle
    }

您在 addSpindle 方法中使用了 self.bounds。由于视图尚未在 init 上布局,因此尚未确定其边界。

您可以在 init 上创建您的主轴视图,但您需要在计算父级边界时在 layoutSubviews 上设置它们的框架。