我是Mac开发的新手,我们有像imagev = [NSArray arrayWithObjects ]这样的方法吗?
我需要一些东西,就像我们在iOS里做的一样,
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中,我如何动画
问候
发布于 2015-07-30 15:52:37
我发现有人在用处理这个问题的核心动画方法,这对我来说已经足够近了。我稍微修改了一下。你需要@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];
}发布于 2017-01-27 09:03:29
以Ben Flynn的伟大答案为基础。
在Swift 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.
}发布于 2012-12-28 06:46:20
可可没有像animatedImageWithImages:duration:这样的东西。Cocoa中的图像可以在空间(不同的分辨率)和颜色深度上有所变化,但不是时间;单个图像总是静态图像,而不是动画。
(动画GIF可能有例外,但GIF不能显示每帧255或256种颜色,并且不支持部分透明。此外,我还没有尝试使用NSImage或CGImage机器创建或显示GIF。)
你需要做的不是创建一个图像,而是一部电影。然后,添加图片到电影,改变每一个的持续时间,以达到你想要的播放速度。在电影观中显示您的电影,可以选择使用隐藏控制器。
https://stackoverflow.com/questions/14064328
复制相似问题