I want to attach a WebView to the cursor when the user hovers a button. And remove it when the mouse exits.
Even when the cursor moves inside the button I want to WebView to keep following the cursor.
Any ideas on how to perform this?
Here's an example on how it should be:
so you have a NSButton ... subclass THAT so you attach a view:
#interface ButtonWithWebViewOnHover : NSButton
#property(strong) WebView *webView;
#end
override mouseEntered and mouseExited there and toggle hidden
...... wait.... we seem to be reinventing the wheel
use NSPopover (from apple directly, but not as graphically flexible as the next:)
or MAAttachedWindow (http://mattgemmell.com/2007/10/03/maattachedwindow-nswindow-subclass/)
You can subclass WebView, and consider that to draw it this method is called:
- (void)drawRect:(NSRect)dirtyRect;
Ao if you call [super drawInRect: dirctyRect] in this method, the view will be normally drawn, otherwise nothing will be drawn.So you can see if the mouse is over the view and decide if to draw it or not.
To resize it, instead you can use this method:
- (void)setBounds:(NSRect)boundsRect;
To detect mouse events you shall implement methods like mouseDown (see NSResponder) in your main view.
Related
I ran into a transition problem today. It's hard to explain to I recorded a very short clip.
I want the first transition (when I click on the + button) to look exactly like the second one (when I click on
Here's the clip:
transition clip
this is my code :
My Source Code
How are you doing it now then? You didn't give a lot of info but you're using a navigation controller right? Do you need to? One option is to present the second screen from the first. In that case you handle the transitions yourself and you could even give the two view controllers a common base class if several components (like the + sign) are common to both. I have some code I can post if that might be useful...
To present and dismiss a vc:
Have a second view controller as a property in your root view controller:
#property (nonatomic, strong) UIViewController *secondViewController;
in the button pressed method for your plus button:
[self presentViewController:_secondViewController animated:YES completion:nil];
in your presented view controller, in the button pressed method for your back button:
[self dismissViewControllerAnimated:YES completion:^{
}];
Also, if you want to slide horizontally as in this case you'll need to do some custom animation, by creating an animator object that is a UIVIewControllerAnimatedTransitioning delegate. I recommend you read up on that - I also have some examples of that for you if you wish.
Cheers.
I'd like to display an NSWindow when right clicking an item in an NSTableView, similarly to how the available outlets are shown in Interface Builder when you right click an object:
Unfortunately you can only use an NSMenu subclass as the menu property.
I also didn't find a delegate method of NSTableView that notifies about right clicks.
I was able to subclass NSTableView and implement rightMouseDown: and rightMouseUp: to be notified about those events, but if I set the menu property of the row cells to nil, they are not highlighted when right clicked, even though I call the super implementation):
- (void)rightMouseDown:(NSEvent *)theEvent {
[super rightMouseDown:theEvent];
NSPoint eventLocation = [theEvent locationInWindow];
eventLocation = [self convertPoint:eventLocation fromView:nil];
NSInteger rowIndex = [self rowAtPoint:eventLocation];
NSLog(#"Right clicked at row index %d", rowIndex);
}
I would like to have the highlight effect in the image below but display a window instead of the context menu:
First for the right click: explicitly select the row on right click (e.g. via this message). Then create your own NSWindow descendant, set an own NSView class as contentView and in the view you can draw the black background, rounded borders and what not. Show this window in your right click handler.
You can use an NSPopover, which works quite nicely. A popover creates a window for you, even if it is somewhat hidden. You'll get it from your controls if you send them the window message, and can register to listen for events, for instance.
The whole popover can be created in IB, and just have to implement the showRelativeToRect:ofView:preferredEdge: method in code.
To catch the right click event, you can use rightMouseDown:, which is originally defined in NSResponder, but is overridden in NSView to simply catch the event and show menu and it doesn't pass the event upwards in the responder chain (or the inheritance chain, for that matter). Hence, you simply implement that method to call showRelativeToRect:ofView:preferredEdge:.
You will typically need to have the contents in an NSViewController and its own accompanying nib file.
The NSPopover's contentViewController property can be set in IB, too.
All in all, not much code needed.
This tutorial is useful.
I'm attempting to create a custom view that contains a play/pause button, and I'll attach any number of these to an NSWindow. I started by creating my own NSView and just drawing out the various pieces, then subclassing the play/pause button as an NSView (baby steps).
This all worked fine until I decided my button needed to extend NSButtonCell rather than an NSView. The following (from TimeSlipView.m) fails miserably, and I can't seem to figure out why:
playPauseButton = [[TimeSlipViewButton alloc] init];
[playPauseButton setButtonType:NSMomentaryPushInButton];
[self addSubview:playPauseButton];
I get a compile error and this warning for that last line: "Incompatible pointer types sending 'TimeSlipViewButton *__strong' to paremeter of type 'NSView *'".
I have a feeling I've misunderstood something very basic, and that for some reason I can't just pass addSubview: my NSButtonCell from within an NSView.
TimeSlipView.h
#import <Cocoa/Cocoa.h>
#import "TimeSlipViewButton.h"
#interface TimeSlipView : NSView {
TimeSlipViewButton *playPauseButton;
NSView *timerText;
NSView *clientText;
NSView *projectText;
NSView *taskText;
}
#end
TimeSlipViewButton.h
#import <Cocoa/Cocoa.h>
#interface TimeSlipViewButton : NSButtonCell
#end
A Cell Is no View and thus cannot be used as such! what you do doesn't work
You try exactly that when adding it as a subview
Cells are a (legacy) concept where views were too expensive.
The were/are used by some controls (like NSButton) to handle the actually drawing.
the Button CONTAINS a button cell
It ISNT a button cell, it IS a NSView
what you might wanna do is give a stock NSButton a specific ButtonCell that has custom drawing options. There are good tutorials out there for given existing NSButtons/NSSegmentedCells/NSTextFields custom NSCells
I have two NSButtons with images for both their on states and off states. Only one should be active at a time; click one and then click the other to change a property back and forth.
The problem is that if I disable a button when it's clicked so it cannot be clicked again, then the image is dimmed when the button is disabled--and I don't want it dimmed, I just want to use the alternate image.
On the other hand, if I just leave the button enabled, but programmatically just don't run any code when it is clicked, then there's a flashing effect as the mouse clicks--which is distracting, when the button should not do anything.
So I either need to prevent the button from being dimmed when it is disabled, or prevent it from changing the button appearance while the mouse button is held down.
After reading up, it sounds like I need to subclass NSButtonCell and override - (BOOL)imageDimsWhenDisabled to do the former. But I can't figure out exactly how to subclass it (what sort of NSButtonCell class I should inherit from) and if the "setCell" method of NSButton is enough to use the new NSButtonCell class, or if I need to subclass NSButton as well.
Some tips on that would be appreciated, or perhaps there's a completely different approach that would achieve my objectives.
Check this out:
[btnInfo.cell setImageDimsWhenDisabled:NO];
When you want to disable it without changing appearance do this:
On MacOS - NSButton:
Only option is to subclass NSButton and override mouseDown function
class RadioButton: NSButton {
override func mouseDown(with event: NSEvent) {}
}
On iOS - UIButton:
Simple disable UserInteraction
mybutton.isUserInteractionEnabled = false
For a more up to date answer in Swift, this works for me:
(theButton.cell! as! NSButtonCell).imageDimsWhenDisabled = false
I have an NSTextField label and I want something to happen when a user clicks on it.
I thought I could create an IBAction and link this to the label but nothing seems to happen.
Any suggestions?
Intentions:
The reason why I am doing this is because I want a label that is a hyperlink to a folder. Perhaps I am taking the wrong approach altogether?
IBAction definition in my PersonController.m
- (IBAction)surnameLabelSelected:(id)sender {
NSLog(#"This should do something!");
}
XIB File
In the XIB file I have made a Received Actions connection between surnameLabelSelected and the StaticText NSTextField label.
You've got a couple options. Francis's answer is one option. Another option is to subclass NSTextField and override -mouseDown:. Something like this (written off the top of my head, not tested):
#interface ClickableTextField : NSTextField
#end
#implementation ClickableTextField
- (void)mouseDown:(NSEvent *)theEvent
{
[self sendAction:[self action] to:[self target]];
}
#end
If NSTextField is closer in style to the appearance you need, this might be the better approach. If you need NSButton's features (highlight upon click, etc) go with Francis's solution.
Labels are non-editable text fields thus don't send actions to their targets. You want to use an NSButton and turn off its border-drawing and size it to fit its text as best as possible to simulate a label.
Another option is to create a transparent button (without title/image/border, momentary push in) in front of the label, with the same frame as label's. And set button's action accordingly.