I am a newcomer to Objective-C, with a little bit of experience in development but not much.
I am currently learning about custom drawing, handling views and understanding View Controllers.
I am simply trying to make an app where a rectangle changes color when I touch it. I got the color changing part alright, but my current problem is that I want the color to change only when I touch inside the rectangle. What happens at the moment is that the color changes wherever I touch the screen.
Here's my code: NICRectangleView.m
#import "NICRectangleView.h"
@implementation NICRectangleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.rectColor = [UIColor redColor];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGRect bounds = CGRectMake(50, 50, 200, 100);
UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:bounds];
[self.rectColor setFill];
[rectPath fill];
}
-(void)setRectColor:(UIColor *)rectColor
{
_rectColor = rectColor;
[self setNeedsDisplay];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%@ was touched", self);
if (self.rectColor == [UIColor redColor]) {
self.rectColor = [UIColor blueColor];
} else {
self.rectColor = [UIColor redColor];
}
}
@end
NICRectangleViewController.m:
#import "NICRectangleViewController.h"
#import "NICRectangleView.h"
@interface NICRectangleViewController ()
@end
@implementation NICRectangleViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)loadView
{
NICRectangleView *rectView = [[NICRectangleView alloc]init];
self.view = rectView;
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
And my AppDelegate.m:
#import "NICAppDelegate.h"
#import "NICRectangleView.h"
#import "NICRectangleViewController.h"
@implementation NICAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
NICRectangleViewController *rvc = [[NICRectangleViewController alloc]init];
self.window.rootViewController = rvc;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Am I right when thinking that it is because when I set the windows rootViewController to my RectangleViewController, it automatically sets it so that the view managed by the controller is full screen?
How can I change this?
Thanks a lot in advance for reading this.
Best regards,
NC