【问题标题】:Detecting Touch of a UIView in UIScrollView layer with an array of UIViews使用 UIViews 数组检测 UIScrollView 层中 UIView 的触摸
【发布时间】:2011-09-15 20:03:08
【问题描述】:

请原谅我在这方面有点陌生。

我正在尝试检测类似于 MoveMe 示例的触摸——只有我有一个 UIViews (studentCell) 数组放入名为 studentCellArray 的 NSMutableArray 中。

[self.studentCellArray addObject:self.studentCell];

当我有一个触摸时,我想让程序足够聪明,以知道它是否触摸了数组中的任何 UIView 以及它是否需要做一些事情。

这是 touchesBegan: 方法中的代码。

//prep for tap
int ct = [[touches anyObject] tapCount];
NSLog(@"touchesBegan for ClassRoomViewController tap[%i]", ct);
if (ct == 1) {
    CGPoint point = [touch locationInView:[touch view]];
    for (UIView *studentCard in self.studentCellArray) {
        //if I Touch a Card then I grow card...

    }
    NSLog(@"I am here[%@]", NSStringFromCGPoint(point));
}

我不知道如何访问视图并触摸它们。

【问题讨论】:

    标签: uiview uiscrollview touch touchesbegan cgpoint


    【解决方案1】:

    我通过将 UIPanGestureRecognizer 分配给数组中的每个 UIView 来“解决”了这个问题。

    这可能不是最好的方法,但我现在可以在屏幕上移动它们。

    代码如下:

    for (int x = 0; x < [keys count]; x++) {
                    UIPanGestureRecognizer *pGr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragging:)];
                    UIView *sca =  [self.studentCellArray objectAtIndex:x];
    
                    [sca addGestureRecognizer:pGr];
                    [pGr release];
                }
    

    这是我使用的“拖动”方法。我已将屏幕分成三部分,如果 UIView 碰巧超过阈值,则会有一个动画将其捕捉到一个点。我希望这能给某人一些好主意。如果您能找到更好的方法,请提供帮助。

    - (void) dragging:(UIPanGestureRecognizer *)p{
    UIView *v = p.view;
    
    if (p.state == UIGestureRecognizerStateBegan || p.state == UIGestureRecognizerStateChanged) {
        CGPoint delta = [p translationInView:studentListScrollView];
        CGPoint c = v.center;
        c.x += delta.x;
        //c.y += delta.x;
        v.center = c;
        [p setTranslation:CGPointZero inView:studentListScrollView];
    
    }
    if (p.state == UIGestureRecognizerStateEnded) {
        CGPoint pcenter = v.center;
        //CGRect frame = v.frame;
        CGRect scrollFrame = studentListScrollView.frame;
        CGFloat third = scrollFrame.size.width/3.0;
        if (pcenter.x < third) {
            pcenter = CGPointMake(third/2.0, pcenter.y);
            //pop the view
            [self showModalDialog:YES perfMode:YES andControlTag:[studentCellArray indexOfObjectIdenticalTo:p.view]];
        }
        else if (pcenter.x >= third && pcenter.x < 2.0*third) {
            pcenter = CGPointMake(3.0*third/2.0, pcenter.y);
    
        }
        else 
        {
            pcenter = CGPointMake(5.0 * third/2.0, pcenter.y);
            //pop the view
            [self showModalDialog:YES perfMode:YES andControlTag:[studentCellArray indexOfObjectIdenticalTo:p.view]];
        }
    
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.2];
        v.center = pcenter;
        [UIView commitAnimations];
    }
    

    }

    编辑:将 [studentCellArray indexOfObjectIdenticalTo:p.view] 添加到 andControlTag 为我提供了所触摸视图数组中的位置,因此我可以将其传递到我的模态对话框中以呈现适当的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 2016-02-06
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多