【问题标题】:How to detect touch between several UIViews?如何检测几个 UIView 之间的触摸?
【发布时间】:2014-06-16 15:22:22
【问题描述】:

情况是我在主屏幕上有几个 UIView,我将它们像表格单元一样一一排列。 我想实现一个功能,我随机点击一个 UIView 并在它们之间移动我的手指(比如悬停)。当我触摸与上次触摸不同的 UIView 时,设备会振动。

直到现在我都在尝试使用“

  • (void)touchesMoved" 方法来实现它,但是没有用。我给每个uiview一个特定的标签。当我调用"-(void)touchesBegan"方法时,我会更新当前的标签号。我希望“- (void)touchesMoved”方法可以帮助我刷新视图的当前标签,但它没有。它与我在“- (void)touchesBegan”方法中更新的标签号保持一致。李>

这是代码。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    touch = [[event allTouches] anyObject];
    currentTag = touch.view.tag;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    touch = [[event allTouches] anyObject];
    NSLog(@"%ld", (long)touch.view.tag);
    if (touch.view.tag != currentTag) {
        NSLog(@"Vibrate");
        currentTag = touch.view.tag;
    }
}

希望得到一些帮助~

谢谢你。

【问题讨论】:

  • 触摸事件后你是否重新加载视图?子视图是出口还是行动?
  • 不,我没有重新加载视图,子视图只是插座。

标签: ios iphone objective-c uiview touch


【解决方案1】:

将您的视图添加到容器视图并覆盖它的 touchesMoved 等并自己检查边界:

-(void)touchesMoved: (NSSet *)touches withEvent: (UIEvent *)event {
    CGPoint location = [[touches anyObject] locationInView:self];

    for (UIView *view in self.subviews) {
        if(CGRectContainsPoint(view.frame, location)){
            if (view.tag != currentTag) {
                NSLog(@"Vibrate");
                currentTag = touch.view.tag;
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    您也可以使用滚动视图来执行此操作。 您可以在滚动视图中创建图块。请在下面找到代码。

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)]; scrollView.backgroundColor=[UIColor clearColor]; [self.view addSubview:scrollView];

    int column = 0;
    for(int i = 0; i < data.count; ++i) {
    
    
        UIView *tile = [[UIView alloc]initWithFrame:CGRectMake(column*230+24, 20, 200, 160)]];
    
        tile.backgroundColor = [UIColor clearColor];
        tile.tag = i+1 ;
    
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
        tapGesture.numberOfTapsRequired = 1;
        [tile addGestureRecognizer:tapGesture];
    
    
        [scrollView addSubview:tile];
    
    
        column++;
    
    
    
    }
    
    
    [scrollView setContentSize:CGSizeMake(column*225+24,view.frame.size.height)];
    

    }

    如果需要任何说明,请告诉我。祝您编码愉快

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      相关资源
      最近更新 更多