gpt4 book ai didi

macos - CALayer 与 NSScrollView,缩放平移和单击

转载 作者:行者123 更新时间:2023-12-04 17:18:38 24 4
gpt4 key购买 nike

我有一个 CALayer 托管 View ,我希望能够对其进行缩放、滚动和单击。如果我将它嵌入到 ScrollView 中,滚动工作正常,但缩放会导致内容随处可见。如果我不将它嵌入到 ScrollView 中,我可以使用 CATransform3DMakeScale 很好地进行缩放,但是随后我在平移和选择对象方面遇到了问题。

解决这个问题的推荐方法是什么 - 在我尝试用错误的选项解决之前。

(如果您有任何做类似事情的来源,我将非常感激看到它。)

最佳答案

我的灵感来自 Apple sample codes

还看Optimizing Drawing and Scrolling on OS X WWDC 2013 video
CALayer嵌入 ScrollView飞因为ScrollView不是核心动画层( ScrollView.wantsLayer = true )。
Interface Builder

这里以 Swift 为例:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var win: NSWindow!

func applicationDidFinishLaunching(aNotification: NSNotification) {

win = NSWindow(contentRect: NSRect(x: 200, y: 200, width: 500, height: 500),
styleMask: NSTitledWindowMask,
backing: NSBackingStoreType.Buffered,
defer: false)

win.makeKeyAndOrderFront(win)

let scrollView = NSScrollView()
scrollView.allowsMagnification = true
scrollView.hasHorizontalScroller = true
scrollView.hasVerticalScroller = true
scrollView.wantsLayer = true

scrollView.contentView = CenteredClipView()
scrollView.documentView = MyView()
scrollView.backgroundColor = NSColor.controlColor()

win.contentView = scrollView
}
}

class MyView: NSView {
var drawn = false

override func updateLayer() {
super.updateLayer()

if !drawn {
drawn = true

frame = (superview! as NSView).frame

var shape = CAShapeLayer()
let p = CGPathCreateMutable()
CGPathAddEllipseInRect(p, nil, frame)
CGPathMoveToPoint(p, nil, bounds.maxX, 0)
CGPathAddLineToPoint(p, nil, 0, bounds.maxY)
shape.path = p
shape.lineWidth = 5
shape.fillColor = NSColor.whiteColor().CGColor
shape.strokeColor = NSColor.selectedControlColor().CGColor
shape.lineDashPattern = [5, 5]

let ant = CABasicAnimation(keyPath: "lineDashPhase")
ant.fromValue = 0
ant.toValue = 1000000
ant.duration = 10000
ant.repeatCount = 10000
shape.addAnimation(ant, forKey: "lineDashPhase")

layer?.addSublayer(shape)
}
}
}

class CenteredClipView: NSClipView {
override func constrainBoundsRect(proposedBounds: NSRect) -> NSRect {
var rect = super.constrainBoundsRect(proposedBounds)

if let containerView = documentView? as? NSView {
if rect.size.width > containerView.frame.size.width {
rect.origin.x = (containerView.frame.width - rect.width ) / 2
}

if rect.size.height > containerView.frame.size.height {
rect.origin.y = (containerView.frame.height - rect.height ) / 2
}
}

return rect
}
}

关于macos - CALayer 与 NSScrollView,缩放平移和单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27442185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com