0

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

我正在尝试检测像 MoveMe 示例这样的触摸——只有我将一组 UIView(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));
}

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

4

1 回答 1

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];
            }

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

- (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 为我提供了所触摸视图数组中的位置,因此我可以将其传递到我的模态对话框以呈现适当的信息。

于 2011-09-16T13:17:34.267 回答