【问题标题】:touchesBegan and responders in iOSiOS 中的 touchesBegan 和响应者
【发布时间】:2014-03-22 02:12:01
【问题描述】:

如何向对象添加事件识别器?

我有以下代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touches happened");


    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.mainView];

    for (UIView *view in self.mainView.subviews)
    {
        if ([view isKindOfClass:[ViewController class]] &&
            CGRectContainsPoint(view.frame, touchLocation))
        {
            NSLog(@"my Touch Happened");
            UIImageView *tmp = (UIImageView *)view;
            if (selectedItem != tmp)
                [self setSelected:tmp];

        }
    }
}

我的目标是,如果我的图像被触摸,而不是被点击或长按但完全被触摸,则执行从 NSLog(@"found whats being touched"); 开始的逻辑

我以前使用过 UIGestureRecognizers,你可以将它们添加到任何你想要的地方。

[self.SomeImage addGestureRecognizer:rotate];

但是我如何告诉我的对象听这个代码呢?

我想知道触摸的区域是否与另一个对象存在的区域相同?也就是说,如果用户试图平移一个项目。我可以处理平底锅。但出锅前的第二个是触摸。我该如何找到那个触摸以及与之相关的对象!

【问题讨论】:

    标签: ios objective-c uitouch touchesbegan


    【解决方案1】:

    UIViewController 和 UIView 直接支持- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    您可以直接将您的函数放入此类对象中。 (即 UIView 或 UIViewController)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多