Cocoa: NSToolBar with Custom Views. Animation issues - cocoa

So I got a Window:
Window.xib
I got a WindowController too:
WindowController.h reads:
#import <Cocoa/Cocoa.h>
#interface MainWindowController : NSWindowController
{
IBOutlet NSView *firstView;
IBOutlet NSView *secondView;
IBOutlet NSView *thirdView;
int currentViewTag;
}
-(IBAction)switchView:(id)sender;
#end
And the WindowController.m reads:
#import "MainWindowController.h"
#interface MainWindowController ()
#end
#implementation MainWindowController
-(id)init
{
self = [super initWithWindowNibName:#"MainWindow"];
if (self){
// Initialization code here
}
return self;
}
//- (void)windowDidLoad {
// [super windowDidLoad];
//
// // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
//}
#pragma mark - Custom view drawing
-(NSRect)newFrameForNewContentView:(NSView *)view
{
NSWindow *window = [self window];
NSRect newFrameRect = [window frameRectForContentRect:[view frame]];
NSRect oldFrameRect = [window frame];
NSSize newSize = newFrameRect.size;
NSSize oldSize = oldFrameRect.size;
NSRect frame = [window frame];
frame.size = newSize;
frame.origin.y -= (newSize.height - oldSize.height);
return frame;
}
-(NSView *)viewForTag:(int)tag{
NSView *view = nil;
if (tag == 0) {
view = firstView;
} else if (tag == 1) {
view = secondView;
} else {
view = thirdView;
}
return view;
}
-(BOOL) validateToolbarItem:(NSToolbarItem *)item
{
if ([item tag] == currentViewTag) return NO;
else return YES;
}
-(void)awakeFromNib
{
[[self window] setContentSize:[firstView frame].size];
[[[self window] contentView]addSubview:firstView];
[[[self window] contentView]setWantsLayer:YES];
}
-(IBAction)switchView:(id)sender
{
int tag = [sender tag];
NSView *view = [self viewForTag:tag];
NSView *previousView = [self viewForTag:currentViewTag];
currentViewTag = tag;
NSRect newFrame = [self newFrameForNewContentView:view];
[NSAnimationContext beginGrouping];
if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask)
[[NSAnimationContext currentContext] setDuration:1.0];
[[[[self window]contentView]animator]replaceSubview:previousView with:view];
[[[self window]animator]setFrame:newFrame display:YES];
[NSAnimationContext endGrouping];
}
#end
The problem I have is that when i switch tabs in my app, the custom view (and there are three of different sizes) draw differently each time. Look at the screenshots, all of the numbers should be centre aligned but they are sometimes and others not. Can anyone see what my error is please?
I will also add that all of the actions have been correctly configured + the code works perfectly if the custom view size is the same all the time.
The view that works
The view that almost works
Again pointing out that in my .xib all of the numbers are aligned to 0x and 0y axis.
Appdelegate.h
#import <Cocoa/Cocoa.h>
#class MainWindowController;
#interface AppDelegate : NSObject <NSApplicationDelegate> {
MainWindowController *mainWindowController;
}
#end
Appdelegate.m
#interface AppDelegate ()
#property (nonatomic) IBOutlet NSWindow *window;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
-(void)awakeFromNib
{
if(!mainWindowController){
mainWindowController = [[MainWindowController alloc]init];
}
[mainWindowController showWindow:nil];
}
#end

In Interface Builder, make sure to disable the "autoresizes subviews" checkbox of the default view of your window

Related

mouseExited is no longer called after mouseDown in NSButton subclass

In building a custom NSButton, I've run into a problem handling highlight behavior. After clicking down on the button, holding, and dragging the cursor outside the button's bounds, mouseExited: and mouseEntered: events are not delivered. I understand the reason why, because in mouseDown: calling [super mouseDown:event]; will block until the click is released.
In researching this I came across this Stack Overflow post which describes the same problem. The solution noted is to add NSTrackingEnabledDuringMouseDrag to the NSTrackingArea options, which I have done, yet I continue to see this problem. I tried the other proposed solution with handling the next events in a loop, but this resulted in odd behavior. The button text color turns black on mouse down instead of highlighting the dimmed color, and it doesn't unhighlight upon releasing the mouse, it remains black.
I am using Xcode 9.3, running on macOS 10.13.4.
Here is my NSButton subclass:
#interface BorderlessButton : NSButton {
NSColor *_tempColor;
}
#property (strong, nonatomic) NSColor *color;
#end
#interface BorderlessButton ()
#property (nonatomic) BOOL pressed;
#end
#implementation BorderlessButton
- (id)init {
if (self = [super init]) {
[self setUp];
}
return self;
}
- (id)initWithFrame:(NSRect)frameRect {
if (self = [super initWithFrame:frameRect]) {
[self setUp];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self setUp];
}
return self;
}
- (void)setUp {
_color = [NSColor redColor];
[self setTitle:self.title];
[self setButtonType:NSButtonTypeMomentaryChange];
[self setBordered:NO];
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:self.bounds
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingEnabledDuringMouseDrag
owner:self
userInfo:nil];
[self addTrackingArea:area];
}
- (void)setTitle:(NSString *)title {
[super setTitle:title];
NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[self attributedTitle]];
[colorTitle addAttributes:#{NSFontAttributeName: self.font, NSForegroundColorAttributeName: self.color} range:NSMakeRange(0, [colorTitle length])];
[self setAttributedTitle:colorTitle];
}
- (void)setColor:(NSColor *)color {
_color = color;
[self setTitle:self.title];
}
- (void)mouseDown:(NSEvent *)event {
self.pressed = YES;
[self highlight:YES];
[super mouseDown:event]; // this blocks until released
[self mouseUp:event];
}
- (void)mouseUp:(NSEvent *)event {
self.pressed = NO;
[self highlight:NO];
[super mouseUp:event];
}
//FIXME: Not called after mouse press down and hold then exit
- (void)mouseExited:(NSEvent *)event {
if (self.pressed) {
[self highlight:NO];
}
[super mouseExited:event];
}
- (void)mouseEntered:(NSEvent *)event {
if (self.pressed) {
[self highlight:YES];
}
[super mouseEntered:event];
}
- (void)highlight:(BOOL)flag {
if (flag) {
if (self.isEnabled) {
NSColor *dimmedColor = [self dimmedColor];
_tempColor = _color;
self.color = dimmedColor;
[self setTitle:self.title];
}
} else {
if (self.isEnabled) {
self.color = _tempColor;
[self setTitle:self.title];
}
}
}
- (NSColor *)dimmedColor {
return [self.color colorWithAlphaComponent:0.5];
}
#end

Draggable NSTextField in Cocoa

I am developing an application to print Invoices. I write the following code to make the NSTextFields Draggable and match with Invoice-fields. It Works, but when a resize the window, the NSTextField, return to initial position. What can i do?
#interface DragTextField : NSTextField <NSWindowDelegate>
#property (readwrite) NSPoint location;
#end
#implementation DragTextField
#synthesize location;
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
}
- (BOOL) acceptsFirstMouse:(NSEvent *)theEvent
{
return YES;
}
- (void)mouseDown:(NSEvent *)theEvent
{
location = [theEvent locationInWindow];
self.location = [[self superview] convertPoint:[theEvent locationInWindow] fromView:nil];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint newDragLocation = [[self superview] convertPoint:[theEvent locationInWindow] fromView:nil];
NSPoint thisOrigin = [self frame].origin;
thisOrigin.x += (-self.location.x + newDragLocation.x);
thisOrigin.y += (-self.location.y + newDragLocation.y);
[self setFrameOrigin:thisOrigin];
self.location = newDragLocation;
}
- (void)mouseUp:(NSEvent *)theEvent {
[self setNeedsDisplay:YES];
}
You have to re-position your textfield whenever the window resizes by using delegate functions.
- (void)windowDidResize:(NSNotification *)notification{
//Change the position of your textfield depending on the window size
}

Changing view in NSWindow removes contents of view

I am using an NSToolbar and NSWindowController to change the views of an NSWindow. When the toolbar items are selected, the view is successfully changed for the window and the window changes it's size according to the view size. Upon the initial loading of the window, the contents of the view appear as expected. However, once a toolbar item is selected the contents of the new view are not visible and when the view is switched back to the original view, it's contents are no longer visible either. Not sure what is causing this so any help would be appreciated.
#import <Cocoa/Cocoa.h>
#interface WindowController : NSWindowController {
IBOutlet NSView *firstView;
IBOutlet NSView *secondView;
int currentViewTag;
}
- (IBAction)switchView:(id)sender;
#end
and
#import "WindowController.h"
#interface WindowController ()
#end
#implementation WindowController
- (id)init {
self = [super initWithWindowNibName:#"WindowController"];
if (self) {
}
return self;
}
- (void)windowDidLoad {
[super windowDidLoad];
}
- (NSRect)newFrameForNewContentView:(NSView*)view {
NSWindow *window = [self window];
NSRect newFrameRect = [window frameRectForContentRect:[view frame]];
NSRect oldFrameRect = [window frame];
NSSize newSize = newFrameRect.size;
NSSize oldSize = oldFrameRect.size;
NSRect frame = [window frame];
frame.size = newSize;
frame.origin.y -= (newSize.height - oldSize.height);
return frame;
}
- (NSView *)viewForTag:(int)tag {
NSView *view = nil;
switch (tag) {
case 0:
view = firstView;
break;
case 1:
view = secondView;
break;
}
return view;
}
- (BOOL)validateToolbarItem:(NSToolbarItem *)item {
if ([item tag] == currentViewTag) return NO;
else return YES;
}
- (void)awakeFromNib {
[[self window] setContentSize:[firstView frame].size];
[[[self window] contentView] addSubview:firstView];
[[[self window] contentView] setWantsLayer:YES];
}
- (IBAction)switchView:(id)sender {
double tag = [sender tag];
NSView *view = [self viewForTag:tag];
NSView *previousView = [self viewForTag:currentViewTag];
currentViewTag = tag;
NSRect newFrame = [self newFrameForNewContentView:view];
[NSAnimationContext beginGrouping];
if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask)
[[NSAnimationContext currentContext] setDuration:1.0];
[[[[self window] contentView] animator] replaceSubview:previousView with:view];
[[[self window] animator] setFrame:newFrame display:YES];
[NSAnimationContext endGrouping];
}
#end
I tried out your code, and what I saw was not that the views disappeared, but that they were positioned wrongly and moved out of view. I fixed this by turning off auto layout in IB, and deselecting all the struts and springs in the size inspector. I also deselected the window's "restorable" property so that if you closed the program with view 2 visible, and reopened, the window (with view 1 inside) would be the correct size for view 1.

Need help debugging switchChanged method

Am getting error ("switchChanged" undeclared) in the implementation file, but can't find the problem. Can you help me?
TIA
ViewController.m
#import "Control_FunViewController.h"
#implementation Control_FunViewController
#synthesize nameField;
#synthesize numberField;
#synthesize sliderLabel;
#synthesize leftSwitch;
#synthesize rightSwitch;
#synthesize doSomethingButton;
-(IBAction)sliderChanged:(id)sender
{
UISlider *slider = (UISlider *)sender;
int progressAsInt = (int)(slider.value + 0.5f);
NSString *newText = [[NSString alloc] initWithFormat:#"%d",progressAsInt];
sliderLabel.text = newText;
[newText release];
}
-(IBAction)textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
}
-(IBAction)backgroundTap:(id)sender
{
[nameField resignFirstResponder];
[numberField resignFirstResponder];
}
-(IBAction)toggleControls:(id)sender
{
if ([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
{
leftSwitch.hidden = NO;
rightSwitch.hidden = NO;
doSomethingButton.hidden = YES;
}
else {
leftSwitch.hidden =YES;
rightSwitch.hidden =YES;
doSomethingButton.hidden = NO;
}
-(IBAction)switchChanged:(id)sender
{
UISwitch *whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
[leftSwitch setOn:setting animated:YES];
[rightSwitch setOn:setting animated:YES];
}
-(IBAction)buttonPressed
{
//TODO: Implement Action Sheet and Alert
}
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[nameField release];
[numberField release];
[sliderLabel release];
[leftSwitch release];
[rightSwitch release];
[doSomethingButton release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#define kSwitchesSegmentIndex 0
#interface Control_FunViewController : UIViewController {
UITextField *nameField;
UITextField *numberField;
UILabel *sliderLabel;
UISwitch *leftSwitch;
UISwitch *rightSwitch;
UIButton *doSomethingButton;
}
#property(nonatomic,retain)IBOutlet UITextField *nameField;
#property(nonatomic,retain)IBOutlet UITextField *numberField;
#property(nonatomic,retain)IBOutlet UILabel *sliderLabel;
#property(nonatomic,retain)IBOutlet UISwitch *leftSwitch;
#property(nonatomic,retain)IBOutlet UISwitch *rightSwitch;
#property(nonatomic,retain)IBOutlet UIButton *doSomethingButton;
-(IBAction)textFieldDoneEditing:(id)sender;
-(IBAction)backgroundTap:(id)sender;
-(IBAction)sliderChanged:(id)sender;
-(IBAction)toggleControls:(id)sender;
-(IBAction)switchChanged:(id)sender;
-(IBAction)buttonPressed;
#end
The colon (:) is part of the name of the method, but you haven't included it in the error message. It may be that you just forgot, but if you're calling -switchChanged (no colon) from somewhere, or if you connected a control using the action -switchChanged (no colon), that's the problem. Perhaps you added the colon and sender parameter later on?

How to display unarchived custom NSView objects

I’m learning Cocoa programming, and I’m unable to figure out how to
archive and unarchive a custom NSView subclass
I’ve made a toy application that presents a window. This window
contains an instance of my custom BackgroundView class (archived in
the xib file). Clicking anywhere in this BackgroundView creates and
displays a blue square whose origin is the click point. This square is
an instance of my Square class. All of this works as I expect.
The Square class implements the NSCoding protocol. I’ve added
dataOfType: typeName: error: and readfromData: ofType: error: methods
to the MyDocument class. As far as I can tell from log statements, the
Squares are being archived to the file, and unarchived when the file
is loaded. But I’m unable to make the squares display themselves on
the window. The Square class’s drawWithRect: method is never called,
even though I’ve called setNeedsDisplay:YES on each square.
The code is as follows:
Square.h
#import <Cocoa/Cocoa.h>
#interface Square : NSView <NSCoding> {
}
#end
Square.m
#import "Square.h"
#implementation Square
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
return self;
}
- (void)drawRect:(NSRect)rect {
[[NSColor blueColor] set];
NSBezierPath *newPath = [NSBezierPath bezierPathWithRect:[self bounds]];
[newPath fill];
}
-(void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeRect:[self frame] forKey:#"frame"];
}
-(id)initWithCoder:(NSCoder *)coder
{
NSRect theRect = [coder decodeRectForKey:#"frame"];
self = [super initWithFrame:theRect];
return self;
}
#end
-----
BackgroundView.h
#import <Cocoa/Cocoa.h>
#interface BackgroundView : NSView {
}
#end
BackgroundView.m
#import "BackgroundView.h"
#import "Square.h"
#implementation BackgroundView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)rect {
for (Square *subview in self.subviews)
[subview setNeedsDisplay:YES];
}
-(void)mouseDown:(NSEvent *)theEvent {
NSPoint unsetClick = [theEvent locationInWindow];
NSPoint theClick = [self convertPoint:unsetClick fromView:nil];
NSRect theRect;
theRect.origin = theClick;
theRect.size.width = 100.00;
theRect.size.height = 100.00;
Square *newSquare = [[Square alloc] initWithFrame:theRect];
[self addSubview:newSquare];
[newSquare setNeedsDisplay:YES];
}
#end
------
MyDocument.h
#import <Cocoa/Cocoa.h>
#class BackgroundView;
#interface MyDocument : NSDocument
{
IBOutlet BackgroundView *theBackgroundView;
}
#end
MyDocument.m
#import "MyDocument.h"
#implementation MyDocument
- (id)init
{
self = [super init];
return self;
}
- (NSString *)windowNibName
{
return #"MyDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
return [NSKeyedArchiver
archivedDataWithRootObject:[theBackgroundView subviews]];
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain
code:unimpErr userInfo:NULL];
}
return nil;
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName
error:(NSError **)outError
{
[theBackgroundView setSubviews:[NSKeyedUnarchiver
unarchiveObjectWithData:data]];
[theBackgroundView setNeedsDisplay:YES];
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain
code:unimpErr userInfo:NULL];
}
return YES;
}
#end
Since Square is a subclass of NSView, and NSView also implements NSCoding, you need to invoke super's implementation of both encodeWithCoder: and initWithCoder:.
#implementation Square
- (id)initWithFrame:(NSRect)frame
{
return [super initWithFrame:frame];
}
-(id)initWithCoder:(NSCoder *)coder
{
if ((self = [super initWithCoder:coder]))
{
NSRect theRect = [coder decodeRectForKey:#"frame"];
self = [super initWithFrame:theRect];
return self;
}
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeRect:[self frame] forKey:#"frame"];
}
- (void)drawRect:(NSRect)rect
{
[[NSColor blueColor] set];
[[NSBezierPath bezierPathWithRect:[self bounds]] fill];
}
#end
As I can see you don't add any of the initWithXXX and encodeWithXXX methods since you don't have custom member variables.
I suggest to check out Apple's Sketch Example.

Resources