gpt4 book ai didi

objective-c - NSSearchField 中的可点击搜索图标和褪色背景

转载 作者:行者123 更新时间:2023-12-03 17:55:44 26 4
gpt4 key购买 nike

我想制作一个 NSSearchField,就像 Ecoute 3.0 中的那样。搜索字段中的搜索图标应该是可点击的,并且在点击时可以展开和折叠。

enter image description here enter image description here

如何实现这一目标?我搜索了文档,但没有找到任何有用的信息。我可以使用动画师代理来实现扩展,所以这很清楚。

另一件事是背景随着展开/折叠而淡入/淡出。我怎样才能实现这个目标?

如果您有任何建议、示例代码等,我将不胜感激。

编辑

好的,我自己找到了第一部分。

- (void) resetSearchButtonCell {
NSButtonCell *c= [[NSButtonCell alloc] init];
[c setButtonType:NSMomentaryChangeButton]; // configure the button
[c setBezelStyle:NSRegularSquareBezelStyle]; // configure the button
[c setBordered:NO];
[c setBezeled:NO];
[c setEditable:NO];
[c setImagePosition:NSImageOnly];
[c setImage:[NSImage imageNamed:@"search"]];
[c setAlternateImage:[NSImage imageNamed:@"search-pushed"]];
[c setTarget:self];
[c setAction:@selector(_search:)];
[self setSearchButtonCell:c];
}

现在,我创建了一个属性 isCollapsed,我可以在 _search: 中切换该属性。在 setIsCollapsed: setter 方法中,我执行以下操作:

NSRect newRect = self.frame;

if (_isCollapsed) {
newRect.size.width = kCollapsedWidth;
} else {
newRect.size.width = kEXpandedWidth;
}

[[self animator] setFrameSize:newRect.size];

但由于某种原因,当 NSSearchField 获得或失去焦点时,框架将重置为初始大小,即 Interface Builder 中设置的大小。

谁能告诉我为什么?

编辑

这个问题我也解决了。这是自动布局,这弄乱了动画。现在,唯一剩下的就是背景 alpha,它应该改变。我可以重画整个东西,但是还有另一种解决方案,我可以只修改背景和取消按钮的 Alpha 吗?

最佳答案

我想做类似的事情。 ITSearchField 非常可靠且漂亮。然而,一旦获得关注,我希望扩大搜索范围。人们可能很快就会发现 NSSearchfield 由许多组件和层组成。

这是我为让它发挥作用而写的内容......注意:它适用于 10.7 或更高版本。此外,您还必须将 IB 中搜索字段控件宽度的布局约束连接到 socket 。

.h

@interface MySearchField : NSSearchField 
@end

.m

#pragma mark - Implementation: Search Field Control

@interface MySearchField()
@property (readwrite, nonatomic, getter=isExpanded) BOOL expand;
@property (readwrite, nonatomic, getter=hasFocusRing) BOOL focusRing;
@property (readwrite, strong, nonatomic) NSTimer *expandTimer;
@property (readwrite, strong, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;
@property (readwrite, nonatomic) CGFloat searchWidth;
@end

static NSTimeInterval const kSearchFieldTime = 0.5;

@implementation MySearchField

@synthesize expand, focusRing;
@synthesize expandTimer, searchWidth;
@synthesize widthConstraint;

#pragma mark Timer Methods:

- (void) resizeSearchField {


if ( self.hasFocusRing ) {

if ( !self.isExpanded ) {

self.searchWidth = self.widthConstraint.constant;
[[self.widthConstraint animator] setConstant:(self.searchWidth * 2)];
self.expand = YES;

} } else {

if ( (self.isExpanded) &&
((!self.stringValue) || ([self.stringValue isEqualToString:@""])) ) {

[[self.widthConstraint animator] setConstant:self.searchWidth];
self.expand = NO;
} }
}

- (void) stopTimer {

if ( self.expandTimer )
[self.expandTimer invalidate];

self.expandTimer = nil;
}

- (void) startTimer {

[self stopTimer];

SEL resizeSelector = @selector(resizeSearchField);
NSMethodSignature *expandSignature = [MySearchField instanceMethodSignatureForSelector:resizeSelector];
NSInvocation *expandInvocation = [NSInvocation invocationWithMethodSignature:expandSignature];
[expandInvocation setSelector:resizeSelector];
[expandInvocation setTarget:self];

self.expandTimer = [NSTimer scheduledTimerWithTimeInterval:kSearchFieldTime invocation:expandInvocation repeats:NO];
}

#pragma mark Override Methods:

- (void) noteFocusRingMaskChanged {

self.focusRing = !NSEqualRects(NSZeroRect, self.focusRingMaskBounds);
[self startTimer];
[super noteFocusRingMaskChanged];
}

@end

好吧,确实是这样。当对焦环发生变化时您会收到通知并启动快速计时器方法。当您单击搜索字段的取消按钮重置控件时,计时器是必需的。计时器所做的唯一事情就是触发 resizeSearchField 方法。

更新:哦,是的,不要忘记将 IB 中的搜索字段控件的类设置为 MySearchField 或任何您想要的名称。

享受吧!

关于objective-c - NSSearchField 中的可点击搜索图标和褪色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13797685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com