在 Objective c/cocoa touch 中查找接触点

发布于 2024-08-06 12:08:07 字数 545 浏览 3 评论 0原文

我在找到接触点时遇到问题。

我有一个函数 loadView(),其中设置了 16 个图块(16 UIImageView)。

然后在函数中:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    // Retrieve the touch point

    UITouch *touch=[[event allTouches]anyObject];
    CGPoint point= [touch locationInView:touch.view];
    CGRect frame = [self frame];  // Error this Line
}

我使用框架来识别使用框架原点按下哪个框架/图块。

但这句话:

CGRect frame = [self frame];

让我发疯。请有人告诉我该怎么做。(解释以及为什么不起作用)。请。

I have a problem to find the touch point.

I have a function loadView() where I set 16 tiles(16 UIImageView).

Then In function:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    // Retrieve the touch point

    UITouch *touch=[[event allTouches]anyObject];
    CGPoint point= [touch locationInView:touch.view];
    CGRect frame = [self frame];  // Error this Line
}

I have Used frame to identify which frame/tiles is pressed using frame-origin.

But this line:

CGRect frame = [self frame];

makes me crazy. Plz someone tell me What to do.(with explanation and why not working). Plz.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦萦几度 2024-08-13 12:08:07

使用这个:

UITouch *touch=[[event allTouches]anyObject];
CGPoint point= [touch locationInView:self.view];

Use this :

UITouch *touch=[[event allTouches]anyObject];
CGPoint point= [touch locationInView:self.view];
十六岁半 2024-08-13 12:08:07

看起来您正在尝试在 UIViewController 子类中的方法中执行此操作。该行可能应该是:

[self.view frame];

It seems like you're trying to do this in a method in a UIViewController subclass. The line should probably be:

[self.view frame];
乄_柒ぐ汐 2024-08-13 12:08:07

我仍然有点不清楚你想要什么,但让我们看看这是否对你有帮助。

如果您要移动不同的对象,那么为什么不创建另一个类并使用 UIView 继承它并在子类中定义触摸功能。
示例标题

...
Tile : UIView
...

示例类实现

...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:[self superview]];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:[self superview]];
    self.center = touchPoint;
}
...

,或者如果您只需要 UIViewController 中的触摸点。

UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:[self.view]];

I am still a bit unclear on what you want, but let's see if this helps you out.

If you are moving different objects then why don't you create another class and inherit it with UIView and define the touch functions in the child class.
Example Header

...
Tile : UIView
...

Example class implementation

...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:[self superview]];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:[self superview]];
    self.center = touchPoint;
}
...

or else if you just want the touch points in a UIViewController.

UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:[self.view]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文