在我的 Catalyst 应用程序中,我NSColorPanel
使用 Mac Helpers 存储库中的代码生成了一个回调。
@interface IPDFMacColorPanel : NSObject
+ (void)showColorPanelWithColorChangeHandler:(void(^)(UIColor *color))colorChangeHandler;
/// Hides the color picker panel and removes the colorChangeHandler observer
+ (void)hide;
+ (void)removeColorChangeHandlerObserver;
@end
@interface NSColorPanel_Catalyst : NSObject
+ (instancetype)sharedColorPanel;
- (void)makeKeyAndOrderFront:(id)sender;
- (void)orderFront:(id)sender;
- (void)orderOut:(id)sender;
- (void)setTarget:(id)target;
- (void)setAction:(SEL)action;
- (UIColor *)color;
@end
@interface IPDFMacColorPanel ()
@property (nonatomic,copy) void(^colorChangeHandler)(UIColor *color);
@end
@implementation IPDFMacColorPanel
+ (instancetype)sharedPanel
{
static IPDFMacColorPanel *panel = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
panel = [IPDFMacColorPanel new];
});
return panel;
}
+ (NSColorPanel_Catalyst *)colorPanel
{
return [NSClassFromString(@"NSColorPanel") performSelector:@selector(sharedColorPanel)];
}
+ (void)showColorPanelWithColorChangeHandler:(void(^)(UIColor *color))colorChangeHandler
{
IPDFMacColorPanel *observer = [IPDFMacColorPanel sharedPanel];
observer.colorChangeHandler = colorChangeHandler;
NSColorPanel_Catalyst *colorPanel = [self colorPanel];
[colorPanel setTarget:observer];
[colorPanel setAction:@selector(colorChange:)];
[colorPanel orderFront:nil];
}
+ (void)hide
{
[[self colorPanel] orderOut:nil];
[self removeColorChangeHandlerObserver];
}
+ (void)removeColorChangeHandlerObserver
{
[IPDFMacColorPanel sharedPanel].colorChangeHandler = nil;
}
- (void)colorChange:(NSColorPanel_Catalyst *)colorPanel
{
if (self.colorChangeHandler) self.colorChangeHandler(colorPanel.color);
}
@end
我像这样产生它:
IPDFMacColorPanel.show { (color) in
if let color = color {
self.saveColor(color)
}
}
然后将其写入 Realm:
public func saveColor(_ color: UIColor) {
try! realm.write {
let converted = ColorUtility.convert(color: color)
if let green = converted["green"] {
self.restaurant.green = green
}
if let red = converted["red"] {
self.restaurant.red = red
}
if let blue = converted["blue"] {
self.restaurant.blue = blue
}
if let alpha = converted["alpha"] {
self.restaurant.alpha = alpha
}
}
}
然而,发生的事情是,因为我收到这么多回调,Realm 正在写很多颜色,导致颜色面板滞后。有什么想法可以解决这个问题吗?