0

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

  • Look there: http://stackoverflow.com/questions/14856906/touchesbegan-touchesended-touchesmoved-for-moving-uiview to know where you "touch", and act as you want. – Larme Aug 19 '14 at 10:07

1 Answers1

0

Try something like this

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
  // Detect touch anywhere
  UITouch *touch = [[event allTouches]  anyObject];

  // Get the specific point that was touched
  CGPoint point = [touch locationInView:self.view]; 
  NSLog(@"pointx: %f pointy:%f", point.x, point.y);

  // See if the point touched is within these rectangular bounds
  if (CGRectContainsPoint(CGRectMake(50, 50, 200, 100), point))
  {
    //do something...
  } 
}

Hope it works.

sabyasachi
  • 74
  • 3
  • Cool, worked perfectly! The only thing is, I had to change [touches anyObject] to [[event allTouches] anyObject] in order for it to work, otherwise it wouldn't recognize the coordinates of the touch. Anyway, thank you for the fast and accurate answer! – user3928933 Aug 19 '14 at 10:28