最有效的动画调整NSImageView大小的方法

时间:2013-03-30 21:00:08

标签: objective-c cocoa

我正在尝试在0.1秒的动画块中将NSImageView缩放/调整为其原始大小的120%,然后在动画块完成后返回其原始大小。什么是最快和最少cpu密集的方式?

我基本上在寻找与iOS中使用的以下代码相当的代码,但我需要它用于Mac应用程序:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.1];
image.transform = CGAffineTransformScale(image.transform, newSize, newSize);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.1];
image.transform = CGAffineTransformScale(image.transform,  originalSize, originalSize);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

可能会占用一点CPU,具体取决于图像是否在NSImageView内缩放,但您可以使用NSAnimationContext完成相同的操作:

NSRect oldFrame = self.imageView.frame;
NSRect newFrame = NSMakeRect(self.imageView.frame.origin.x,
                             self.imageView.frame.origin.y,
                             self.imageView.frame.size.width*1.2f,
                             self.imageView.frame.size.height*1.2f);

[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
    // Animating to the new frame...
    [context setDuration:0.1f];
    [[self.imageView animator] setFrame:newFrame];
} completionHandler:^{
    [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
        // Back to the old frame...
        [context setDuration:0.1f];
        [[self.imageView animator] setFrame:oldFrame];
    } completionHandler:nil];
}];

但这种方式可以在OS X 10.7及更高版本上运行。

这也可以通过看起来有点像UIView代码的方式完成,并且可以使用10.5及更高版本:

- (void)whatever {
    NSRect oldFrame = self.imageView.frame;
    NSRect newFrame = NSMakeRect(self.imageView.frame.origin.x,
                                 self.imageView.frame.origin.y,
                                 self.imageView.frame.size.width*1.2f,
                                 self.imageView.frame.size.height*1.2f);

    [self performSelector:@selector(animateImageViewToFrame:)
               withObject:[NSValue valueWithRect:newFrame]
               afterDelay:0];
    [self performSelector:@selector(animateImageViewToFrame:)
               withObject:[NSValue valueWithRect:oldFrame]
               afterDelay:0.1f];
}



- (void)animateImageViewToFrame:(NSValue*)frameValue {
    [NSAnimationContext beginGrouping];
    [[NSAnimationContext currentContext] setDuration:0.1f];
    // Get smaller
    [[self.imageView animator] setFrame:[frameValue rectValue]];
    [NSAnimationContext endGrouping];
}

当然,如果有效的话会很棒:

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.1f];
[[self.imageView animator] setFrame:newFrame];
[NSAnimationContext endGrouping];

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.1f];
[[self.imageView animator] setFrame:oldFrame];
[NSAnimationContext endGrouping];

甚至更好:

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.1f];
[[self.imageView animator] setFrame:newFrame];
[[self.imageView animator] setFrame:oldFrame];
[NSAnimationContext endGrouping];

但是,由于我无法弄清楚的原因,这些都没有做任何事情。

有关详细信息,请参阅NSAnimationContext Class Reference