LoginSignup
0
0

More than 1 year has passed since last update.

NSButtonでアニメーションをつけてGIF画像を表示

Last updated at Posted at 2022-02-16

概要

  • 下記のようにNSButtonにアニメーションのGIF画像を表示する。

image

方針

  • NSButtonが持つプロパティはNSImageViewではなくNSImage。なのでSDWebImage/SDWebImage Publicを使い、NSButtonSDAnimatedImageViewaddSubViewすることで実現する。

参考

import Cocoa
import SDWebImage

class ViewController: NSViewController {

    @IBOutlet weak var imageButton: NSButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        SDWebImageManager.shared.loadImage(with: URL(fileURLWithPath: "/Users/ikeh/Downloads/ImageSample/giphy.gif"), options: [], progress: nil) { loadedImage, _, _, _, _, _ in
            guard let loadedImage = loadedImage else { return }
            let animatedImageView = SDAnimatedImageView(image: loadedImage)
            animatedImageView.animates = true
            self.imageButton.addSubview(animatedImageView)
            animatedImageView.anchor(top: self.imageButton.topAnchor, left: self.imageButton.leftAnchor, right: self.imageButton.rightAnchor, bottom: self.imageButton.bottomAnchor, paddingTop: 2, paddingLeft: 2, paddingRight: 2, paddingBottom: 2)
        }
    }

    @IBAction func imageButtonClicked(_ sender: Any) {
        print("imageButtonClicked")
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0