2013-03-30 42 views
3

我試圖在0.1秒的動畫塊中將NSImageView縮放/調整爲原始大小的120%,然後在動畫塊完成後返回到原始大小。什麼是最快和最少cpu密集的方式呢?最有效的方式來動畫調整大小的NSImageView

基本上,我在尋找的代碼等同於在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]; 

感謝您的幫助。

+0

爲每個AppKit對象提供的動畫代理? – CodaFi

回答

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]; 

但無論是那些做任何事情,理由THA我無法弄清楚。

查看NSAnimationContext Class Reference瞭解更多信息。

+0

據我所知NSAnimationContext可以使用很多CPU的能力,還有其他一些可能嗎? – lvp