一个简单的图片“翻书”,可以以翻书风格动画呈现。
DSFImageFlipbook 将翻书中的每张图片在图片的帧持续时间内呈现给翻书的所有者(使用 block 回调或 Combine Publisher)。
支持:-
我想在 Dock 磁贴中放置一个动画 GIF。 由于 NSDockTile
的特性,你不能直接播放一个 NSImageView
作为磁贴的 contentView
- Dock 磁贴只有在你告诉它的时候才会更新(通过 display()
)。
所以我需要一个类,可以在定义的时间呈现给我单独的图像,以便我可以手动更新 Dock 图像。
let flipbook = DSFImageFlipbook()
...
// Add frames manually
flipbook.addFrame(image: image1, duration: 0.5) // first frame for 0.5 seconds
flipbook.addFrame(image: image2, duration: 0.5) // second frame for 0.5 seconds
flipbook.addFrame(image: image3, duration: 0.5) // third frame for 0.5 seconds
flipbook.addFrame(image: image4, duration: 2.0) // last frame for 2.0 seconds
// And start the animation
flipbook.start() { [weak self] (frame, current, count, stop) in
// Do something with the provided frame
}
let flipbook = DSFImageFlipbook()
// Load the frames from a gif
let gif = Bundle.main.image(forResource: NSImage.Name("animation.gif"))!
_ = flipbook.loadFrames(from: gif)
...
// Use a combine sink to listen to frame changes in localFlipbook
cancellable = flipbook.publisher
.map { return $0.image }
.sink(receiveValue: { [weak self] image in
... do something with 'image'
})
// Start the animation at 2x the speed defined in the flipbook. Repeat 10 times.
flipbook.start(speed: 2, repeatCount: 10)
MIT。 可以随意使用和滥用它,只需注明我的工作。 如果您在某个地方使用了它,请告诉我,我很乐意听到它!
MIT License
Copyright (c) 2021 Darren Ford
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.