4

我是 Mac 开发的新手,我们有没有像 imagev = [NSArray arrayWithObjects

我需要一些像我们在 iOS 中做的事情想在 mac 中做的事情,

imageVie.animationImages = [NSArray arrayWithObjects:
 [UIImage imageNamed:@"1.png"],[UIImage imageNamed:@"2.png"],
 [UIImage imageNamed:@"3.png"],[UIImage imageNamed:@"4.png"],
 [UIImage imageNamed:@"5.png"],[UIImage imageNamed:@"6.png"],
 [UIImage imageNamed:@"7.png"] ,nil];

在 iPhone 中,我如何制作动画

问候

4

5 回答 5

4

我发现有人使用Core Animation 方法来解决这个问题,这对我来说已经足够接近了。我稍微修改了一下。你需要@import QuartzCore;

- (void)awakeFromNib
{
    CALayer *layer = [CALayer layer];
    NSMutableArray *spinnerImages = [NSMutableArray arrayWithCapacity:30u];
    for (NSUInteger i = 0; i < 30; ++i)
    {
        NSString *imageName = [NSString stringWithFormat:@"spinner%@", @(i)];
        [spinnerImages addObject:[NSImage imageNamed:imageName]];
    }
    self.spinnerImages = spinnerImages;
    layer.frame = self.imageView.bounds;
    [self.imageView setLayer:layer]; // This view is just a container for the layer. Its frame can be managed by a xib.
    self.imageView.wantsLayer = YES;

    self.spinnerLayer = layer;
}

然后你可以像这样动画它:

- (void)stopAnimating
{
    if ([self.layer.animationKeys containsObject:kAnimationKey])
    {
        [self.layer removeAnimationForKey:kAnimationKey];
    }
}

- (void)startAnimating
{
    if ([self.layer.animationKeys containsObject:kAnimationKey])
    {
        return;
    }
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:kAnimationKey];
    [animation setCalculationMode:kCAAnimationDiscrete];
    [animation setDuration:1.0f];
    [animation setRepeatCount:HUGE_VALF];
    [animation setValues:self.spinnerImages];
    [self.spinnerLayer addAnimation:animation forKey:kAnimationKey];
}
于 2015-07-30T15:52:37.767 回答
2

以Ben Flynn的出色回答为基础。

在斯威夫特 3 中:

// This needs to happen around init.
// NSView (and who inherit from it) does not come with a layer.
// self.layer will be nil at init.
self.layer = CALayer()
self.wantsLayer = true

let layer = CALayer()
let keyPath = "contents" // (I did not find a constant for that key.)
let frameAnimation = CAKeyframeAnimation(keyPath: keyPath)
frameAnimation.calculationMode = kCAAnimationDiscrete

// Duration
// This is the duration of one cycle
let durationOfAnimation: CFTimeInterval = 2.5
frameAnimation.duration = durationOfAnimation
frameAnimation.repeatCount = HUGE// can also use Float.infinity

let imageSeq: [NSImage] = imageSequance // Your images to animate
frameAnimation.values = imageSeq

// Sadly, there are no layout constraints on CALayer.
// If your view will be resized while animating, you'll need to override
// 'func layout()' and calculate aspect ratio if needs be
let layerRect = CGRect(origin: CGPoint.zero, size: self.frame.size)
layer.frame = layerRect
layer.bounds = layerRect
layer.add(frameAnimation, forKey: keyPath)

self.layer?.addSublayer(layer)

如果预计视图会调整大小:

删除这些行:

let layerRect = CGRect(origin: CGPoint.zero, size: self.frame.size)
layer.frame = layerRect
layer.bounds = layerRect

self.needsLayout = true在添加子层后调用。这将导致layout()被调用。

//let layerRect = CGRect(origin: CGPoint.zero, size: self.frame.size)
//layer.frame = layerRect
//layer.bounds = layerRect
layer.add(frameAnimation, forKey: keyPath)

self.layer?.addSublayer(layer)
self.needsLayout = true

最后,覆盖layout()

override func layout() {
    super.layout()

    var layerFrame = CGRect(origin: CGPoint.zero, size: self.frame.size)
    self.myLayer.frame = layerFrame
    self.myLayer.bounds = // Adjust ratio as needed.
}
于 2017-01-27T09:03:29.927 回答
1

可可没有类似的东西animatedImageWithImages:duration:。Cocoa 中的图像可以在空间(不同的分辨率)和颜色深度上有所不同,但时间不一样;单个图像始终是静态图像,从不动画。

(动画 GIF可能有一个例外,但 GIF 每帧不能显示超过 255 或 256 种颜色,并且不支持部分透明。此外,我还没有尝试使用 NSImage 或 CGImage 机制创建或显示 GIF。 )

您需要做的不是创建图像,而是创建电影。将图像添加到电影中,改变每个图像的持续时间以达到您想要的播放速度。然后,在电影视图中显示您的电影,可选择隐藏控制器

于 2012-12-28T06:46:20.033 回答
1

斯威夫特 4.2

在添加beginTime. 因为Swift 3一些常数也发​​生了变化。所以我已将解决方案转换为Swift 4.2. 另外,我认为将其创建为CALayer扩展会很方便:

extension CALayer {  

static func image(sequence: [NSImage], duration: CFTimeInterval? = nil, frame: CGRect? = nil) -> CALayer {

    let layer = CALayer()
    if let f = frame { layer.frame = f }
    layer.autoresizingMask = [.layerWidthSizable, .layerHeightSizable]

    let keyPath = "contents"

    let keyFrameAnimation = CAKeyframeAnimation(keyPath: keyPath)
    keyFrameAnimation.values = sequence
    keyFrameAnimation.calculationMode = .discrete
    keyFrameAnimation.fillMode = .forwards
    keyFrameAnimation.duration = duration ?? CFTimeInterval(sequence.count / 18)
    keyFrameAnimation.repeatCount = Float.infinity
    keyFrameAnimation.autoreverses = false
    keyFrameAnimation.isRemovedOnCompletion = false
    keyFrameAnimation.beginTime = 0.0

    layer.add(keyFrameAnimation, forKey: keyPath)

    return layer

}
}

像这样使用它

let sequenceLayer = CALayer.image(sequence: imageSequence, duration: 0.55, frame: yourView.bounds)
于 2018-12-27T14:12:36.967 回答
0
@interface AnimatedNSImage : NSImage

    @property (nonatomic, retain) IBInspectable NSImageView         *delegate;

    @property (nonatomic, readonly) NSArray                         *frames;
    @property (nonatomic, readonly) CGFloat                         duration;

    - (instancetype) initWithImages:(NSArray*)frames duration:(CGFloat)duration;

@end

在 .m 文件中...

@interface AnimatedNSImage () {

    NSTimer                         *_scheduledTimer;
    NSArray                         *_frames;

    NSUInteger                      _frameIndex;
    CGFloat                         _duration;

}

@end

@implementation AnimatedNSImage

    @synthesize delegate;

    - (NSArray *) frames
    {
        return _frames;
    }

    - (CGImageRef) CGImage
    {
        if (_frames && _frames.count >0) {
            NSImage *_frame = _frames[_frameIndex];
            return _frame.CGImage;
        }
        return nil;
    }

    - (NSArray<NSImageRep *> *) representations
    {
        NSImage *_frame = _frames[_frameIndex];
        return _frame.representations;
    }

    - (CGFloat) duration
    {
        return _duration;
    }

    - (void) __showNextFrame:(id)sender
    {
        _frameIndex = (_frameIndex + 1) % _frames.count;
        if (delegate) {
            [delegate setNeedsDisplay:YES];
        }
    }

    - (NSSize) size
    {
        if (_frames && _frames.count >0) {
            NSImage *_frame = _frames[_frameIndex];
            return _frame.size;
        }
        return CGSizeZero;
    }

    - (void) setup
    {
        _scheduledTimer = [NSTimer scheduledTimerWithTimeInterval:_duration target:self selector:@selector(__showNextFrame:) userInfo:nil repeats:YES];
    }

    - (void) dealloc
    {
        [_scheduledTimer invalidate];
        _scheduledTimer = nil;
    }

    - (instancetype) initWithImages:(NSArray *)frames duration:(CGFloat)duration
    {
        self = [super init];
        if (self) {
            _frames = frames;
            _duration = duration / 100.0f;
            [self setup];
        }
        return self;
    }

@end

请注意,您必须分配委托(给 NSImageView)才能调用刷新。

一个例子....

IBOutlet NSImageView *_testGifView;

AnimatedNSImage *_animatedImage = [NSImage animatedImageWithAnimatedGIFURL:[NSURL URLWithString:@"https://media.giphy.com/media/B2zB8mHrHHUXS57Cuz/giphy.gif"]];
_testGifView.image = _animatedImage;        
_animatedImage.delegate = _testGifView;

当然可以根据需要调整计划的计时器,因为输入时间以厘秒(而不是分钟)为单位。

于 2019-11-21T13:44:20.333 回答