Obj-C - touchesBegan:在定义的区域

时间:2014-08-19 10:05:29

标签: ios objective-c view uiviewcontroller

我是Objective-C的新手,在开发方面有一点经验,但并不多。

我目前正在学习自定义绘图,处理视图和理解视图控制器。

我正在尝试创建一个应用程序,当我触摸它时矩形会改变颜色。我的换色部分没问题,但我目前的问题是我只想在触摸矩形内部时才改变颜色。目前发生的事情是,无论我触摸屏幕,颜色都会发生变化。

这是我的代码: 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.
}

我的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;
}

我认为这是因为当我将windows rootViewController设置为RectangleViewController时,它会自动设置它以便控制器管理的视图是全屏的吗?

我该如何更改?

非常感谢您提前阅读此内容。

致以最诚挚的问候,

NC

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

- (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...
  } 
}

希望它有效。