gpt4 book ai didi

objective-c - 拖动交换 NSView

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

因为你永远不知道,它可能很有用,我一直在尝试编写一个灵活的 NSSplitView 实验应用程序,其中可以以用户想要的任何方式动态添加和删除 View 。我能做到这一点。

现在我认为能够:

  1. 交换 View - 例如,在四 View 窗口中,左上 View 可以拖动到右下角,并且在释放鼠标按钮时, View 将彼此交换。

  2. 将 View 拖出 - 例如,在一个四 View 窗口中,如果将左上角 View 拖出其包含的窗口,那么它将成为其右侧的一个窗口,包含该 View 和原始 View 窗口将变成三 View 窗口。

  3. 将 View 拖入 - 以便可以将窗口拖入 View 中,关闭窗口并将其 View 添加到拖入的窗口中。

我已经编写了一个程序来完成第一步(灵活设置分割 View )https://github.com/HeadBanging/SplitViewTest但我完全不知道如何做剩下的事情 - 特别是第一点。

如果你看一下代码,你会发现我已经开始了(使用 Apple 和其他地方的教程),但它并没有达到我想要的效果。有人有什么建议吗?

当然,如果您需要的只是为您的项目提供灵活的拆分窗口,那么您就可以使用我的窗口(上面下载),没有使用限制 - 一切顺利。

Willeke 对于如何让拖动发挥作用提出了一些很好的建议,我已按如下方式实现了这些建议(Git 上的完整代码):

#pragma mark Dragging

- (NSImage *)imageRepresentationOfView:(NSView*)draggingView {
BOOL wasHidden = draggingView.isHidden;
CGFloat wantedLayer = draggingView.wantsLayer;

draggingView.hidden = NO;
draggingView.wantsLayer = YES;

NSImage *image = [[NSImage alloc] initWithSize:draggingView.bounds.size];
[image lockFocus];
CGContextRef ctx = [NSGraphicsContext currentContext].graphicsPort;
[draggingView.layer renderInContext:ctx];
[image unlockFocus];

draggingView.wantsLayer = wantedLayer;
draggingView.hidden = wasHidden;

return image;
}

- (void)mouseDown:(NSEvent *)theEvent {
NSSize dragOffset = NSMakeSize(0.0, 0.0);
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:self];

DebugView *hitView;
NSPoint startLocation = NSMakePoint(0, 0);
NSImage *draggedImage;
BOOL found = NO;

fHitView = nil;
while ((hitView = [[[self subviews] objectEnumerator] nextObject]) && !found) {
if ([hitView isKindOfClass:[DebugView class]] && [(DebugView *)hitView dragEnabled]) { //Change DebugView to Draggable View, and use as container for plugin views
draggedImage = [self imageRepresentationOfView:hitView];
startLocation = hitView.frame.origin;
found = YES;
}
}
if (draggedImage != nil) {
[pboard setData:[draggedImage TIFFRepresentation] forType:NSTIFFPboardType];

[self dragImage:draggedImage at:startLocation offset:dragOffset
event:theEvent pasteboard:pboard source:self slideBack:YES];
}
return;
}

- (void)setHighlighted:(BOOL)value {
isHighlighted = value;
[self setNeedsDisplay:YES];
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
NSPasteboard *pboard = [sender draggingPasteboard];

if ([[pboard types] containsObject:NSFilenamesPboardType]) {

NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
for (NSString *path in paths) {
NSError *error = nil;
NSString *utiType = [[NSWorkspace sharedWorkspace]
typeOfFile:path error:&error];
if (![[NSWorkspace sharedWorkspace]
type:utiType conformsToType:(id)kUTTypeFolder]) {

[self setHighlighted:NO];
return NSDragOperationNone;
}
}
}
[self setHighlighted:YES];
return NSDragOperationEvery;
}

- (void)draggingExited:(id <NSDraggingInfo>)sender {
[self setHighlighted:NO];
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
[self setHighlighted:NO];

DebugView *hitView;
BOOL found = NO;

fHitView = nil;
while ((hitView = [[[self subviews] objectEnumerator] nextObject]) && !found) {
if ([hitView isKindOfClass:[DebugView class]] && [(DebugView *)hitView dragEnabled]) {
found = YES;
}
}
NSView* tempView = [sender draggingSource];
[[[sender draggingSource] superview] replaceSubview:[sender draggingSource] with:hitView];

[self replaceSubview:hitView with:tempView];
[self setNeedsDisplay:YES];
[[[sender draggingSource] superview] setNeedsDisplay:YES];
return YES;
}

- (BOOL)isHighlighted {
return isHighlighted;
}

放置部分部分有效 - 有时 View 准备接受放置,有时则不接受(有人看到我做错了什么吗? - 它应该一直有效,除非被拖放到的 View 是源 View )。

最后一 block 拼图对我来说仍然是个谜(接受掉落,并交换观点)。任何提示将非常感激地接受。

最佳答案

一个 View 可以有一个 super View ,当您将一个 View 添加到另一个 super View 时,该 View 将从原始 super View 中删除。用 View B 替换 View A,然后用 View A 替换 View B 是行不通的,因为 View ​​B 已从其原始 super View 中删除。

自动布局对我来说仍然是一个谜,但首先删除两个 View 然后添加它们似乎可行:

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
[self setHighlighted:NO];

// swap subviews of view1 and view2
NSView *view1 = self;
NSView *view2 = [sender draggingSource];

// find subviews
DebugView *hitView1, *hitView2;
for (hitView1 in [view1 subviews]) {
if ([hitView1 isKindOfClass:[DebugView class]]) {
break;
}
}
for (hitView2 in [view2 subviews]) {
if ([hitView2 isKindOfClass:[DebugView class]]) {
break;
}
}

// swap hitView1 and hitView2
if (hitView1 && hitView2) {
[hitView1 removeFromSuperview];
[hitView2 removeFromSuperview];
[view1 addSubview:hitView2];
NSDictionary *views = NSDictionaryOfVariableBindings(hitView2);
[view1 addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[hitView2]|"
options:0
metrics:nil
views:views]];
[view1 addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[hitView2]|"
options:0
metrics:nil
views:views]];
[view2 addSubview:hitView1];
views = NSDictionaryOfVariableBindings(hitView1);
[view2 addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[hitView1]|"
options:0
metrics:nil
views:views]];
[view2 addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[hitView1]|"
options:0
metrics:nil
views:views]];
return YES;
}
return NO;
}

关于objective-c - 拖动交换 NSView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38787323/

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