Adding Gradient background to NSViewController CocoaTouch - objective-c

I have created a NSViewController which i have called "GradientViewController" with nib file, I have initialised the view and it is getting displayed over my rootViewController. I am wanting to add a gradient background to "GradientViewController". Does anyone know how i can do this?
if I create a CAGradientLayer with "GradientViewController" using
NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:[NSColor orangeColor] endingColor:[NSColor lightGrayColor]];
How would i get this to display? (If it's the correct way to create a gradient)
It's been very difficult to find the answer to this and i've been searching for days. Example code would be great as I am new to cocoaTouch and trying to swap from iOS

I always just add a custom view class as a subview and put the gradient in the drawRect function of that when I want gradients on my ViewControllers.

Related

how to pop a drawing uiview as a model view only half of the screen

I create new viewcontroller class with xib then created uiview inherited class and in xib file custom class I wrote the uiview inherited class name(PaintView).
I am trying to implement a simple drawing feature in my app, I created a simple drawing code which paints on uiview using touchbegain, thouchmoves, drawrect methods in PaintView class.
Till here its all working fine now I want to pop a model of this view, but I want it to display half of the screen, I resize it in interface builder but it is still showing on full screen.
Other posts says create a transparent view add contents at bottom half thats how you will get half model view, but this is not a solution for my problem.
How to properly pop model view but only half of the screen.
Simple, don't present it modally.
UIViewController *controller = [[UIViewController alloc] initWithNibName:#"myXib" bundle:[NSBundle mainBundle]];
[controller.view setFrame:CGRectMake(0, 480, 320, 320)];
[self.view addSubview:controller.view];
[UIView animateWithDuration:0.5 animations:^{
[controller.view setTransform:CGAffineTransformMakeTranslation(0, -320)];
}];
To change the color's I recommend you store the integer values of your color's hues into an array, that way you can stably assign the hue from the integer value of some object in the array:
arrayOfColors = [[NSArray alloc] initWithObjects:#"0.0", #"0.1", #"0.2", #"0.3", #"0.4", #"0.5", #"0.6", #"0.7", #"0.8", #"0.9", nil];
[[UIColor colorWithHue:[[arrayOfColors objectAtIndex:someIndex]floatValue] saturation:1.0 brightness:1.0 alpha:1.0]setStroke];

Move NSImage inside NSImageview

I'm new to Cocoa and kinda trying to create a timer.
I'm trying to move the NSImage inside the NSImageview up or down based on a timer.
Here is how I load the image inside the NSImageview:
NSRect fnRect = NSMakeRect(10 + (wheelWidth / 4) - 5, 30, numbersWidth, wheelHeight);
fnImageView = [[NSImageView alloc] initWithFrame:fnRect];
[fnImageView setImageScaling:NSScaleNone];
[fnImageView setImage:numbers];
[self addSubview:fnImageView];
If anyone can point me to a tutorial or give me a hint about how to do it I would appreciate it.
Thanks.
Inside a NSImageView isn't a NSImage. It's just used to show the NSImage.
What you want to do is probably a NSImageView inside a NSView where you could move it around.
Create a new NSView
Put an NSImageView in it
Move the NSImageView around.
You might want to look into CoreAnimation and working with layers.
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/WhatisCoreAnimation.html#//apple_ref/doc/uid/TP40004689-SW1

How to add a CALayer to an NSView on Mac OS X

I'm trying to learn how to use and implement CALayer in a Mac Objective-C application, but I can't seem to probably do the most basic thing - add a new layer and set its background colour/frame size. Can anyone see what is wrong with my code?
CALayer *layer = [CALayer layer];
[layer setFrame:CGRectMake(0, 0, 100, 100)];
[layer setBackgroundColor:CGColorCreateGenericRGB(1.0, 0.0, 0.0, 1.0)];
[self.layer addSublayer:layer];
[layer display];
I put this in the - (void)drawRect:(NSRect)rect method of my custom NSView subclass, but when I run the application, it just shows a blank view, with no background colour or evidence of the layer I created.
First of all, you don't want to add a layer in the drawRect: method of a view, this gets called automatically by the system and you'd probably end up with a lot more layers than you actually want. initWithFrame: or initWithCoder: (for views that are in a nib file) are better places to initialize your layer hierarchy.
Furthermore, NSViews don't have a root layer by default (this is quite different from UIView on iOS). There are basically two kinds of NSViews that use a layer: layer-backed views and layer-hosting views. If you want to interact with the layer directly (add sublayers etc.), you need to create a layer-hosting view.
To do that, create a CALayer and call the view's setLayer: method. Afterwards, call setWantsLayer:. The order is important, if you'd call setWantsLayer: first, you'd actually create a layer-backed view.
You need to make a call to the "setWantsLayer" method.
Check out the following documentation for the description for setWantsLayer:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html
In a nutshell, your view needs to be layer-hosting view. Because it is a layer-hosting view, you should interact with the layer, and NOT interact with the view itself and don't add subviews to it.
[self setLayer:[CALayer new]];
[self setWantsLayer:YES]; // the order of setLayer and setWantsLayer is crucial!
[self.layer setBackgroundColor:[backgroundColor CGColor]];
Put this out of the drawRect. I normally put my layer setup in either the init method or the viewDidLoad.
Otherwise anytime the view is drawn a new layer is added and allocated. Also I've never used the [layer display] line before. The docs actually tell you not to call this method directly.
Updated info (Swift): first call view.makeBackingLayer() then set wantsLayer to true.
https://developer.apple.com/documentation/appkit/nsview/1483695-wantslayer

Custom background drawing in an grouped UITableViewCell

I'm having some trouble drawing a custom gradient background in a UITableViewCell when the style is set to 'grouped' and the cell is first or the last one of the section. My approach is to simply create a CAGradientLayer and add it to the view like this:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = rect;
gradient.colors = [NSArray arrayWithObjects:(id)[_backgroundColorLight CGColor], (id)[_backgroundColorDark CGColor], nil];
[self.backgroundView.layer insertSublayer:gradient atIndex:0];
self.backgroundView.layer.masksToBounds = YES;
Unfortunately this produces cells like this one:
Does anyone have a hint on how to make the background fit the boarders of the cell?
Thanks
–f
If you draw a custom background, you also have to draw the borders yourself. There's quite some open source stuff out there.
Basically, you need to know [in the cell] if it's top/middle/bottom/single, and cut the stuff in drawRect. You won't come far with insertSublayer.
Check out the PageCellBackground class you find here:
http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html
I've not tried it but CAGradientLayer happens to be a subclass of CALayer, so perhaps setting its borderRadius may work. But it rounds all corners then, so you may compensate for that by making the layer bigger than the cell and have the cell view cut it off.
Look at Rounded UIView using CALayers - only some corners - How? for more examples

How to draw a simple line without using Interface Builder in iphone sdk?

I want to draw a simple line in view controller with out using Interface builder.
If you only want to draw horizontal or vertical line, you can just use UIView.
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(<startingX>, <startingY>, <width>, <height>)];
lineView.backgroundColor = <desiredColor>;
//assume self is UIViewController or its sublcass
[self.view addSubview:lineView];
[lineView release];
Try this question. I know this references InterfaceBuilder, but you can dynamically add your custom view using Obj-C like mentioned here.