将相邻的跟踪区域视为一个连续区域

时间:2018-08-12 00:52:28

标签: swift3 core-animation nstrackingarea

我试图在鼠标离开窗口的标题或内容视图时显示标题/较少窗口的UI,但是在从一个窗口移到另一个窗口时显示不是;本质上,这两个跟踪区域是一个功能(我诉诸于此,因为在隐藏标题视图时我不知道如何创建单个区域):

override func mouseEntered(with theEvent: NSEvent) {
    let hideTitle = (doc?.settings.autoHideTitle.value == true)
    if theEvent.modifierFlags.contains(.shift) {
        NSApp.activate(ignoringOtherApps: true)
    }

    switch theEvent.trackingNumber {
    case closeTrackingTag:
        Swift.print("close entered")
        closeButton?.image = closeButtonImage
        break

    default:
        Swift.print(String(format: "%@ entered",
                           (theEvent.trackingNumber == titleTrackingTag
                            ? "title" : "view")))
        let lastMouseOver = mouseOver
        mouseOver = true
        updateTranslucency()

        //  view or title entered
        if hideTitle && (lastMouseOver != mouseOver) {
            updateTitleBar(didChange: !lastMouseOver)
        }
    }
}

override func mouseExited(with theEvent: NSEvent) {
    let hideTitle = (doc?.settings.autoHideTitle.value == true)

    switch theEvent.trackingNumber {
    case closeTrackingTag:
        Swift.print("close exited")
        closeButton?.image = nullImage
        break

    default:
        Swift.print(String(format: "%@ exited",
                           (theEvent.trackingNumber == titleTrackingTag
                            ? "title" : "view")))

        let lastMouseOver = mouseOver
        mouseOver = false
        updateTranslucency()

        if hideTitle && (lastMouseOver != mouseOver) {
            updateTitleBar(didChange: lastMouseOver)
        }
    }
}

此外,关闭按钮上还有一个跟踪矩形,仅在结束时才显示。无论如何,从我的跟踪器输出中,我看到了问题-将鼠标从下方移到其标题上方:

view entered
updateTitleBar
**view entered**
view exited
updateTitleBar
title entered
updateTitleBar
title exited

请注意,为什么我会收到第二个进入视图的事件(进入视图),但是移出视图并移至相邻标题会触发一个updateTilebar()调用,该调用在视觉上很不稳定-迄今为止尚未通过动画进行补救:

fileprivate func docIconToggle() {
    let docIconButton = panel.standardWindowButton(.documentIconButton)

    if settings.autoHideTitle.value == false || mouseOver {
        if let doc = self.document {
            docIconButton?.image = (doc as! Document).displayImage
        }
        else
        {
            docIconButton?.image = NSApp.applicationIconImage
        }
        docIconButton?.isHidden = false
        self.synchronizeWindowTitleWithDocumentName()
    }
    else
    {
        docIconButton?.isHidden = true
    }
}

@objc func updateTitleBar(didChange: Bool) {
    if didChange {
        Swift.print("updateTitleBar")
        if settings.autoHideTitle.value == true && !mouseOver {
            NSAnimationContext.runAnimationGroup({ (context) -> Void in
                context.duration = 1.0
                panel.animator().titleVisibility = NSWindowTitleVisibility.hidden
                panel.animator().titlebarAppearsTransparent = true
                panel.animator().styleMask.formUnion(.fullSizeContentView)
            }, completionHandler: {
                self.docIconToggle()
            })
        } else {
            NSAnimationContext.runAnimationGroup({ (context) -> Void in
                context.duration = 1.0
                panel.animator().titleVisibility = NSWindowTitleVisibility.visible
                panel.animator().titlebarAppearsTransparent = false
                panel.animator().styleMask.formSymmetricDifference(.fullSizeContentView)
            }, completionHandler: {
                self.docIconToggle()
            })
        }
    }
}

所以我的问题是当区域相邻时如何推迟动作。

它们(标题栏和内容视图)不是彼此的兄弟姐妹,因此不认为hitTest()是可行的,但基本上,如果我能确定我是否移入了相邻的跟踪区域,我希望一个没有人。

任何有关动画为什么不起作用的帮助都将是一个加分。

1 个答案:

答案 0 :(得分:0)

这不是一个正确的答案,但是如果您知道相邻视图的矩形,则可以使用事件的位置来探查是否要忽略相邻视图之间的移动:

override func mouseExited(with theEvent: NSEvent) {
    let hideTitle = (doc?.settings.autoHideTitle.value == true)
    let location : NSPoint = theEvent.locationInWindow

    switch theEvent.trackingNumber {
    case closeTrackingTag:
        Swift.print("close exited")
        closeButton?.image = nullImage
        break

    default:
        let vSize = self.window?.contentView?.bounds.size

        //  If we exit to the title bar area we're still "inside"
        //  and visa-versa, leaving title to content view.
        if theEvent.trackingNumber == titleTrackingTag, let tSize = titleView?.bounds.size {
            if location.x >= 0.0 && location.x <= (vSize?.width)! && location.y < ((vSize?.height)! + tSize.height) {
                Swift.print("title -> view")
                return
            }
        }
        else
        if theEvent.trackingNumber == viewTrackingTag {
            if location.x >= 0.0 && location.x <= (vSize?.width)! && location.y > (vSize?.height)! {
                Swift.print("view -> title")
                return
            }
        }

        mouseOver = false
        updateTranslucency()

        if hideTitle {
            updateTitleBar(didChange: true)
        }

        Swift.print(String(format: "%@ exited",
                           (theEvent.trackingNumber == titleTrackingTag
                            ? "title" : "view")))
    }
}