Drawing in a nested UIView - iphone

I am trying to make a drawing application following this tutorial: http://www.effectiveui.com/blog/2011/12/02/how-to-build-a-simple-painting-app-for-ios/, and then I tried to make it such that I don't only draw on the entire screen, I only draw on a UIView that is inside of another UIView. (What I call a nested UIView)
My code is currently on github: https://github.com/sammy0025/SimplePaint
The only parts I tweaked with the original code from the tutorial is to change some class prefix names, enabled ARC so no deallocs, used storyboards (which works fine with the original code from the tutorial), and change this code in the main view controller implementation file (SPViewController.m):
-(void)viewDidLoad
{
[super viewDidLoad];
nestedView.backgroundColor=[UIColor colorWithWhite:0 alpha:0]; //This is to make the nested view transparent
SPView *paint = [[SPView alloc] initWithFrame:nestedView.bounds]; //original code is SPView *paint=[[SPView alloc] initWithFrame:self.view.bounds];
[nestedView addSubview:paint]; //original code is [self.view addSubview:paint];
}
My question is how do I make sure that I only draw inside the nested UIView?

Add clipping to your sub view. E.g. self.bounds would cover the whole area of a view.
UIBezierPath *p = [UIBezierPath bezierPathWithRect:self.bounds];
[p addClip];
I believe clipping for a view should be based on bounds initially and that you may shrink it by adding an new clipping. But there might be a difference between iOS and OS X here.

Rather than using an SPView inside a nested view, consider just changing the frame of the SPView to match what you want. This should solve your problem of only allowing drawing within a given rect.
I think you're seeing issues because of your storyboard configuration. I dont know much about storyboard, but I was able to get this to work programmatically by throwing out storyboard's view and building my own in viewDidAppear.
-(void)viewDidAppear:(BOOL)animated{
UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
newView.backgroundColor = [UIColor greenColor];
SPView *newPaint = [[SPView alloc] initWithFrame:CGRectInset(self.view.bounds, 40,40)];
[newView addSubview:newPaint];
self.view = newView;
}

Related

UIview clear a zone

This is an inherited code, I would like to change as few as possible, just give support for iphone5 (taler) screen size.
Somewhere I have a code like this:
[self.view addSubview:leftSwipedViewController.view];
The problem is: the parent UIViewController has filled out the 480x2 -> 568x2 zone ( difference between iphone4 and iphone5) the new swiped UIController will fill the 0->480x2 height zone. I would like to clear the remain space with a nice white color. How to do it?
something like [self.view clearRectangle(0,568-480,320,568, color.white)]; but this function doesn't exist as is.
Is there any better, fast solution? - I don't want to create a xib file for a code where the component are place from within code to X, Y coordinates.
Yes, it's very simple actually
Right before adding your view, do this:
UIView *whiteView = [[UIView alloc] initWithFrame:self.view.bounds];
whiteView.tag = 123; // you'll need it if you want to remove it later.
whiteView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:whiteView];
[self.view addSubview:leftSwipedViewController.view];
When you want to remove it, do this:
[[self.view viewWithTag:123] removeFromSuperview];
However, the correct approach would be to make your leftSwipedViewController support both heights

UITableViewCell background color

I have a UITableViewController that I'm trying to custom style. I've got 95% of the way there, but am having trouble removing an odd shading from the cell background. I've highlighted what I'm trying to remove in this screen shot:
To try and resolve this issue, I've dumped all the views that are composing my UITableView and then colored them to try and find the view I need to change:
As you can see, I've been unable to color that specific spot. Said differently, I can't find a view associated with that spot.
How do I remove that shading from my cells?
The table view is restarting the gradient behind each section, since it is just taking the colour you have given it.
Instead, use the backgroundView property of the table view - create an image view using your image, and set this as the background view, or create a UIView with background colour as you are doing above.
You will need to remove any code you have setting background colours elsewhere, and make sure your section header views either have clear backgrounds, or a contrasting background.
As a simple demonstration, I have created a grouped table view with its background colour set to clear, and the following code in viewDidLoad of the table view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *backgroundView = [[UIView alloc] initWithFrame:self.tableView.bounds];
CAGradientLayer *layer = [CAGradientLayer layer];
layer.colors = [NSArray arrayWithObjects:(id)[UIColor redColor].CGColor,(id)[UIColor whiteColor].CGColor, nil];
layer.frame = backgroundView.frame;
[backgroundView.layer addSublayer:layer];
self.tableView.backgroundView = backgroundView;
}
This gives the following (admittedly, pretty ugly!). The cells "float" on top of this.

Views inside views (iOS, Drawing properties)

Ok, I recognize that I have a complete mess in my mind.
Really, the objective is very simple. I have a little subview inside one of my views (I've added it in Interface Builder). I'm trying to insert inside a little subview that automatically resizes herself to occupy the exact space of his parent view.
Controller *c = [[Controller alloc] initWithNibName:nil bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:c];
[self.containerView addSubview:self.navigationController.view];
[c release];
Controller class contains an UIImageView pretty big. But I've set the properties of the view and the image to 'scale to fill' and size itself properly. However, the image automatically exceeds the area of the view showing bigger and occupying all the window.
Ideas? Thanks a lot!
Additional:
After applying meggar's method I have an strange behaviour. The container view is in position 277,176 with a dimensions of 214w,204h. The inserted view is appearing with the same dimensions but translated 100px (aprox.) to the right.
What kind of effect can be causing it?
setting the view's frame equal to its superview's frame should do the trick, something like:
UIView* parentView = self.navigationController.view.superview;
CGRect myFrame = CGRectMake(0,0,parentView.frame.size.width,parentView.frame.size.height);
[self.navigationController.view setFrame:parentView.myFrame ];
[self.containerView addSubview:self.navigationController.view];

disable elements on UIView behind modal UIView

i have a UIView that is smaller than the superview so i can represent this view as a modal view when a button is clicked.
I have managed to do the following:
* add a subview to the superview.
* centered this modal view
I am now trying to make the elements behind the UIView unclickable. And also add a grey shadow te the ourside of my modal view so that the user understands that the modal view is the view in focus.
I would like to know how to achieve this.
I do not wish to use the presentation modal transition. I know and have already implemented this in other projects.
Any help is appreciated.
The simplest thing would be to lay a fullscreen UIView with a translucent gray background behind your "modal" view. Then it will intercept all of the touches. It might look something like this:
UIView *dimBackgroundView = [[UIView alloc] initWithFrame:theSuperview.bounds];
dimBackgroundView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:.5f];
[theSuperview addSubview:dimBackgroundView];
[theSuperview addSubview:modalView];
For future reference, you can set myView.userInteractionEnabled = NO to disable touch events on a view.
There are several ways to do it.
If you have a custom view which has custom location, you can modify it like that:
Create an instance var:
UIView* backgroundView;
And whenever you need it, put it behind your custom view:
if (backgroundView == nil)
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)];
backgroundView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:.5f];
[self.view addSubview:backgroundView];
[backgroundView animateBump:customView.view];
[backgroundView addSubview:customView.view];
When you do not need it anymore
[backgroundView removeFromSuperview];

Learning the basics of UIScrollView

I've been having a very hard time finding good examples of UIScrollView. Even Apple's UIScrollView Suite I find a bit lacking.
I'm looking for a tutorial or example set that shows me how to create something similar to the iPhone Safari tab scrolling, when you zoom out from one browser window and can flick to others.
But I'm having a hard time just getting any old view showing within a scroll view. I have a view set up with an image in it, but when I add it to the scroll view, I only get a black rectangle, no matter what I put in the view I add.
Any links or code snippets would be great!
Here is a scroll view guide from Apple
The basic steps are:
Create a UIScrollView and a content view you want to put inside (in your case a UIImageView).
Make the content view a subview of the scroll view.
Set the content size of the scrollview to the frame size of the content view. This is a very important step that people often omit.
Put the scroll view in a window somewhere.
As for the paging behavior, check out UIScrollView’s pagingEnabled property. If you need to scroll by less than a whole page you’ll need to play tricks with clipsToBounds, sort of the reverse of what is happening in this StackOverflow question.
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSInteger viewcount= 4;
for (int i = 0; i <viewcount; i++)
{
CGFloat y = i * self.view.frame.size.height;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,self.view.frame.size.width, self .view.frame.size.height)];
view.backgroundColor = [UIColor greenColor];
[scrollview addSubview:view];
[view release];
}
scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *viewcount);
For more information Create UIScrollView programmatically
New 3/26/2013
I stumbled on an easier way to do this without code (contentSize)
https://stackoverflow.com/a/15649607/1705353