CASpringAnimation 从 NSView 的 0.0 锚点开始

CASpringAnimation start from 0.0 anchorpoint of NSView

提问人:Avi Rok 提问时间:7/4/2023 最后编辑:Avi Rok 更新时间:7/5/2023 访问量:35

问:

我正在尝试按用途制作动画,但动画总是从 0.0 锚点开始 可以更改锚点吗?如何将动画更改为从我的中心开始?动画视频NSViewNotchPanelCASpringAnimationcontentViewNSView

    func animationBigger () {
    //SELF IS NSPanel
    let springAnimation = CASpringAnimation(keyPath: "transform.scale")
    springAnimation.damping = 1.0
    springAnimation.mass = 5.0
    springAnimation.initialVelocity = 1.0
    springAnimation.stiffness = 50.0
    springAnimation.fromValue = 0.2
    springAnimation.toValue = 1.0
    springAnimation.duration = 0.6
    self.contentView!.layer!.add(springAnimation, forKey: nil)
    self.setContentSize(bignotchSize)
    self.contentView!.setFrameSize(bignotchSize)
}

class NotchView: NSView {
private var mouseDownSubject = PassthroughSubject<NSPoint, Never>()
var mouseDownPublisher: AnyPublisher<NSPoint, Never> {
    return mouseDownSubject.eraseToAnyPublisher()
}

override func draw(_ dirtyRect: NSRect) {
    super.draw(dirtyRect)
    drawNotch()
}

func drawNotch () {
    let size = self.frame.size
    let r: CGFloat = 16.0
    let gap: CGFloat = 1.0
    let notch = NSBezierPath()
    notch.move(to: NSPoint(x: 0.0, y: size.height))
    notch.curve(to: NSPoint(x: r, y: size.height - r),
                controlPoint1: NSPoint(x: r - gap, y: size.height),
                controlPoint2: NSPoint(x: r, y: size.height - gap))
    notch.line(to: NSPoint(x: r, y: r))
    notch.curve(to: NSPoint(x: 2 * r, y: 0.0),
                controlPoint1: NSPoint(x: r, y: gap),
                controlPoint2: NSPoint(x: r + gap, y: 0.0))
    notch.line(to: NSPoint(x: size.width - 2 * r, y: 0.0))
    notch.curve(to: NSPoint(x: size.width - r, y: r),
                controlPoint1: NSPoint(x: size.width - r - gap, y: 0.0),
                controlPoint2: NSPoint(x: size.width - r, y: gap))
    notch.line(to: NSPoint(x: size.width - r, y: size.height - r))
    notch.curve(to: NSPoint(x: size.width, y: size.height),
                controlPoint1: NSPoint(x: size.width - r, y: size.height - gap),
                controlPoint2: NSPoint(x: size.width - r + gap, y: size.height))
    notch.close()
    NSColor.black.setFill()
    notch.fill()
}

//Tap with mouse
override func mouseDown(with event: NSEvent) {
    super.mouseDown(with: event)
    //move notch to top
    mouseDownSubject.send(event.locationInWindow)
  }
 }
Swift macOS NSView nsPanel 锚点

评论


答: 暂无答案