3

我正在尝试在 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];

谢谢你的帮助。

4

1 回答 1

3

NSImageView它可能会占用一些 CPU ,具体取决于图像是否在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 类参考

于 2013-04-20T05:00:57.200 回答