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 类参考。