我想在我的Mac App中動畫我的按鈕的寬度,同時有一個背景圖像和一個前景圖像。爲此,我使用@ kgn的真棒圖書館BBlock(https://github.com/kgn/BBlock)。在drawInRect for Mac之前清空App
問題在於背景圖像似乎相互牽扯在一起,所以當按鈕縮小時,甚至看不到背景圖像動畫,並且它似乎被切斷。
這種「動畫」寬度變化的方式工作,如果我只是使用setImage,但我沒有得到有背景圖像的好處。
我製作了一個自定義按鈕,可以逐漸縮放按鈕(基本上可以對其進行動畫處理)。它通過一個像素每次運行改變按鈕的尺寸和改變由一個像素的圖像,背景圖像和交替的背景圖像,以及:
- (void)scaleTestButtonUntilWidth:(int)width{
[NSTimer scheduledTimerRepeats:YES withTimeInterval:timeInterval andBlock:^{
if (self.width == width) {
return;
}
int newSize;
if (width > self.width) {
newSize = self.width += 1;
}else if (width < self.width){
newSize = self.width -= 1;
}
self.width = newSize;
[self setImage:self.image];
[self setAlternateBackgroundImage:[self alternateBGImage]];
[self setBackgroundImage:[self BGImage]];
}];
}
該了setBackgroundImage看起來像這樣和setAlternateBackgroundImage實現是相同的:
- (void)setBackgroundImage:(NSImage *)backgroundImage{
[self setImage:[self imageWithBackgroundImage:backgroundImage
andIcon:self.image]];
[self setButtonType:NSMomentaryChangeButton];
[self setBordered:NO];
}
其中要求,實際上繪製圖像的方法:
- (NSImage *)imageWithBackgroundImage:(NSImage *)background andIcon:(NSImage *)icon{
return [NSImage imageForSize:background.size withDrawingBlock:^{
NSRect bounds = NSZeroRect;
bounds.size = background.size;
NSRect iconRect = NSZeroRect;
iconRect.size = icon.size;
iconRect.origin.x = round(background.size.width*0.5f-iconRect.size.width*0.5f);
iconRect.origin.y = round(background.size.height*0.5f-iconRect.size.height*0.5f);
[background drawInRect:bounds fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f];
[icon drawInRect:iconRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f];
}];
}
使用這種方法的幫助:
+ (NSImage *)imageForSize:(NSSize)size withDrawingBlock:(void(^)())drawingBlock{
if(size.width <= 0 || size.width <= 0){
return nil;
}
NSImage *image = [[NSImage alloc] initWithSize:size];
[image lockFocus];
drawingBlock();
[image unlockFocus];
#if !__has_feature(objc_arc)
return [image autorelease];
#else
return image;
#endif
}
沒有建議?在繪製新圖像之前,必須有某種方法可以清除背景。 – 2013-02-20 05:13:03
這聽起來像是你實現NSImage + imageForSize:withDrawingBlock:的方式有問題。我假設你自己實現了一個類別,因爲它不是標準的NSImage方法。 – indragie 2013-02-20 07:05:33