Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在cocoa中转换NSImageView?_Swift_Macos_Cocoa_Animation - Fatal编程技术网

Swift 如何在cocoa中转换NSImageView?

Swift 如何在cocoa中转换NSImageView?,swift,macos,cocoa,animation,Swift,Macos,Cocoa,Animation,我在和可可做斗争。我正试图将这段来自iOS的动画写入cocoa。想法是稍微减小NSImageView的大小,在动画完成后,再次将其增大到原始大小。这样看起来好像按钮(图片)被按下了 @IBOutlet weak var vpnButton: NSImageView! @objc func vpnButtonPressed(pressedGestureRecognizer: UILongPressGestureRecognizer) { UserDefaults.standard

我在和可可做斗争。我正试图将这段来自iOS的动画写入cocoa。想法是稍微减小NSImageView的大小,在动画完成后,再次将其增大到原始大小。这样看起来好像按钮(图片)被按下了

@IBOutlet weak var vpnButton: NSImageView!

@objc func vpnButtonPressed(pressedGestureRecognizer: UILongPressGestureRecognizer) {
        UserDefaults.standard.set(true, forKey: Constants.vpnButtonTapped)
        if (pressedGestureRecognizer.state == .began) {
            UIView.animate(withDuration: 0.15, animations: {() -> Void in
                self.vpnButton?.transform = CGAffineTransform(scaleX: 0.965, y: 0.965)})
        } else if (pressedGestureRecognizer.state == .ended) {
            UIView.animate(withDuration: 0.15, animations: {() -> Void in
                self.vpnButton.isHighlighted = !self.vpnButton.isHighlighted
                self.vpnButton?.transform = CGAffineTransform(scaleX: 1, y: 1)})
        }
    }
在cocoa中,我能够找到点击手势。我不确定这是不是最好的选择

所以我想到了这个:

@objc func vpnButtonPressed(clickedGestureRecognizer: NSGestureRecognizer) {
        UserDefaults.standard.set(true, forKey: Constants.vpnButtonTapped)
        print("clicked")
        NSAnimationContext.runAnimationGroup({_ in
            //Indicate the duration of the animation
            NSAnimationContext.current.duration = 0.5
            var transform = CGAffineTransform(scaleX: 0.965, y: 0.965)
            self.vpnButton.layer?.setAffineTransform(transform)
        }, completionHandler:{
//            var transform = self.vpnButton.layer?.affineTransform()
//            transform = CGAffineTransform(scaleX: 1, y: 1)
//            self.vpnButton.layer?.setAffineTransform(transform!)
            print("Animation completed")
        })
    }

通过将图像稍微移到一边,这只起了一次作用,但不会使图像变小。如果我取消注释完成处理程序中的三行,我也看不到动画将其移回

据我所知,您需要类似于以下内容的内容(如果需要,发送动作本身不在范围内-此处仅为动画)

class MyImageView: NSImageView {

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.wantsLayer = true
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.wantsLayer = true
    }

    override func mouseDown(with event: NSEvent) {
        NSAnimationContext.runAnimationGroup({ (context) in
          context.duration = 0.15
          self.layer?.setAffineTransform(CGAffineTransform(scaleX: 0.965, y: 0.965))
        })
    }

    override func mouseUp(with event: NSEvent) {
        NSAnimationContext.runAnimationGroup({ (context) in
          context.duration = 0.15
          self.layer?.setAffineTransform(.identity)
        })
    }
}