【问题标题】:touchesBegan location issue [duplicate]touchesBegan位置问题[重复]
【发布时间】:2012-02-08 18:15:29
【问题描述】:

我正在更新一些旧代码,并且我有一个子类 UIView (GraphView),它具有以下 touchesBegan 代码:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint point = [[touches anyObject] locationInView:self];
if ([self.grapher.gLayers pointInside:point]) {
NSLog(@"gLayer has point");
... do some stuff
}

// Draw a red dot where the touch occurred
UIView *touchView = [[UIView alloc] init];
[touchView setBackgroundColor:[UIColor redColor]];
touchView.frame = CGRectMake(point.x-5, point.y-5, 10, 10);
touchView.layer.cornerRadius = 5;
[self addSubview:touchView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[touchView setAlpha:0.0];
[UIView commitAnimations];
[touchView release];  
} 

我想将此代码(在 GraphView.m 中运行良好)移动到此视图的 GraphViewController.m 中,但出现 2 个错误。第一个是

self.grapher.gLayers 

生成一个错误,提示“在 UIView 类型的对象上找不到属性图示器。”我将如何在视图控制器中正确编写它?我目前正在使用

self.view.grapher.gLayers

我遇到的第二个错误在线

touchView.layer.cornerRadius = 15;

错误说在前向类对象“CALayer *”中找不到“属性“cornerRadius”。为什么这段代码可以在 UIView 中运行,而在 viewcontroller 中却不行?

【问题讨论】:

    标签: objective-c cocoa-touch uitouch


    【解决方案1】:

    grapher 是你视图的属性吗?

    你导入<QuartzCore/QuartzCore.h>了吗?

    编辑

    好的。您要将其从 GraphView 移动到 GraphViewController? GraphViewController 是否使用 xib?您需要将视图类从 UIView 更改为 GraphView。使用 IB,选择 View,然后将 Custom Class 更改为 GraphView。

    【讨论】:

    • 是的,grapher 是视图的属性。导入 QuartzCore 修复了第二个问题
    • 您的视图控制器需要有一个名为 GraphView 的自定义 UIView。请参阅上面的编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 2021-03-13
    相关资源
    最近更新 更多