小编nin*_*rew的帖子

在光标位置放大NSScrollView

我有一个NSScrollView与自定义NSImageView作为其在xib文件中设置的文档视图.当我想放大NSScrollView时,我的问题出现了.我希望它的行为类似于Mac上的预览应用程序,我可以让它放大得很好,但我希望它以光标为中心点进行放大.

我在自定义NSScrollView类中有这个代码:

//Informs the receiver that the user has begun a pinch gesture.
- (void)magnifyWithEvent:(NSEvent *)event {
NSPoint conViewPoint =[[self contentView] convertPoint:[event locationInWindow] fromView:nil];
[self setMagnification:[event magnification]+[self magnification] centeredAtPoint:conViewPoint];
}
Run Code Online (Sandbox Code Playgroud)

这可行,但它也会导致滚动视图在放大时向下滚动(尽管它根本不会向侧面移动).在没有从触控板抬起的情况下多次放大和缩小时更加明显.

类似的问题似乎是在这个问题,但我使用的是捏手势,而不是按钮.我一直试图偏移y轴滚动,正如该问题所建议的那样

[[self documentView] scrollPoint:scrollPoint]
Run Code Online (Sandbox Code Playgroud)

但我无法找到scrollPoint该帐户的工作公式,以便更改内容视图边界以将光标下的点保持在同一位置.所以,我想知道是否有更合适的方法,或者是否真的需要滚动,我只需要弄清楚数学.

感谢您的帮助

编辑:

所以我终于找到了滚动数学.这需要一段时间和更复杂的尝试,但最终非常简单:

- (void)magnifyWithEvent:(NSEvent *)event {

    NSPoint docViewPoint =[[self documentView] convertPoint:[event locationInWindow] fromView:nil];
    NSPoint docRectOri=[self documentVisibleRect].origin;

    float widthGapSize=-docRectOri.x;
    float heightGapSize=-docRectOri.y;

    if([event phase]==NSEventPhaseBegan){
        startDocPoint=docViewPoint;
        startPoint.x=(docViewPoint.x+widthGapSize)*[self magnification];
        startPoint.y=(docViewPoint.y+heightGapSize)*[self magnification];
    }
    scrollPoint.x=startDocPoint.x-(startPoint.x/[self magnification]);
    scrollPoint.y=startDocPoint.y-(startPoint.y/[self magnification]);

    [self setMagnification:[event magnification]+[self magnification] centeredAtPoint:docViewPoint];
    [[self documentView] …
Run Code Online (Sandbox Code Playgroud)

macos cocoa nsscrollview magnification

6
推荐指数
0
解决办法
667
查看次数

在 NSTextField 中取消选择文本

在开发一个新应用程序时,我遇到了这里描述的相同问题,在取消选择输入到 nstextfield 中的文本时遇到了同样的问题:http : //www.cocoabuilder.com/archive/cocoa/195313-nstextfield-how-to-取消选择-text.html

关于使用 NSTextViews 执行此操作有很多问题,但我无法为 NSTextFields 找到有效的答案。

在我的项目中,我有一个带有文本字段的窗口,其中包含一个控制器类,该类也是文本字段的委托。我有一个 IBAction 用于在输入时发送的文本字段,它根据字段中的文本执行操作,以及:

(void)controlTextDidChange:(NSNotification *)note
Run Code Online (Sandbox Code Playgroud)

(BOOL)control:(NSControl *)control
       textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector 
Run Code Online (Sandbox Code Playgroud)

它处理一些自定义的自动完成,如在Suppressing the text completion dropdown for an NSTextField的答案中

我的问题在于当我按 Enter 提交输入的文本时,字段中的字符串被完全选中,但我希望能够取消选择文本并在末尾添加插入点,如第一个链接中所述。

有多个地方我可以让它取消选择文本,但实际上取消选择不起作用。我已经尝试获取第一个链接中描述的字段编辑器,我也尝试使用 controlTextDidEndEditing: 方法,因为我确实在上面的 controlTextDidChange: 方法中获得了一个字段编辑器:

- (void)controlTextDidEndEditing:(NSNotification *)note{

    NSTextView *textView = [[note userInfo] objectForKey:@"NSFieldEditor"];
    [textView setSelectedRange:NSMakeRange([[self.ParseField stringValue]length]-1, 0)
                      affinity:NSSelectionAffinityUpstream
                stillSelecting:NO];


}
Run Code Online (Sandbox Code Playgroud)

我还尝试在现场禁用和重新启用编辑,但这也不起作用。

像能够向文本字段发送 moveDown: 消息一样简单的事情对我有用,因为它与点击向下箭头相同,但文本字段无法识别该选择器。(我认为 NSTextField 继承自 NSResponder,所以它会起作用,但我猜不是?)

感谢您的任何帮助

macos objective-c nstextfield deselect

4
推荐指数
2
解决办法
3377
查看次数