使用UIView数组检测UIScrollView层中UIView的触摸

使用UIView数组检测UIScrollView层中UIView的触摸,uiview,uiscrollview,touch,touchesbegan,cgpoint,Uiview,Uiscrollview,Touch,Touchesbegan,Cgpoint,原谅我,我对这一点有点陌生 我试图检测一个触摸,就像MoveMe示例一样——只是我有一个UIView数组(studentCell)放入一个名为StudentCellray的NSMutableArray中 [self.studentCellArray addObject:self.studentCell]; 当我有一次触摸时,我想使程序足够智能,以知道它是否触摸了阵列中的任何UIView,以及它是否必须执行某些操作 下面是touchesbeated:method中的代码 //prep for t

原谅我,我对这一点有点陌生

我试图检测一个触摸,就像MoveMe示例一样——只是我有一个UIView数组(studentCell)放入一个名为StudentCellray的NSMutableArray中

[self.studentCellArray addObject:self.studentCell];
当我有一次触摸时,我想使程序足够智能,以知道它是否触摸了阵列中的任何UIView,以及它是否必须执行某些操作

下面是touchesbeated:method中的代码

//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分配一个UIPangestureRecognitor来“解决”这个问题

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

代码如下:

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];
            }
for(int x=0;x<[键计数];x++){
UIPanGestureRecognizer*pGr=[[UIPanGestureRecognizer alloc]initWithTarget:自操作:@selector(拖动:)];
UIView*sca=[self.studentCellArray对象索引:x];
[sca addgestureRecognitor:pGr];
[pGr释放];
}
这是我使用的“拖动”方法。我已经将屏幕分为三个部分,并且有一个动画,如果恰好超过阈值,可以将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];
}
-(无效)拖动:(UIPangestureRecognitor*)p{
UIView*v=p.view;
if(p.state==UIGestureRecognitizerStateStarted | | p.state==UIGestureRecognitizerStateChanged){
CGPoint delta=[p TranslationView:studentListScrollView];
c点=v中心;
c、 x+=δx;
//c、 y+=δx;
v、 中心=c;
[p setTranslation:CGPointZero-inView:studentListScrollView];
}
if(p.state==UIgestureRecognitizerStateEnded){
CGPoint pcenter=v.center;
//CGRect frame=v.frame;
CGRect scrollFrame=studentListScrollView.frame;
CGFloat third=scrollFrame.size.width/3.0;
如果(pcenter.x=third&&pcenter.x<2.0*third){
pcenter=CGPointMake(3.0*third/2.0,pcenter.y);
}
其他的
{
pcenter=CGPointMake(5.0*third/2.0,pcenter.y);
//弹出视图
[self-showModalDialog:YES perfMode:YES and ControlTag:[studentCellArray IndexofObjective:p.view]];
}
[UIView beginAnimations:nil上下文:NULL];
[UIView设置动画持续时间:0.2];
v、 中心=pcenter;
[UIView委员会];
}
}

编辑:将[StudentCellray IndexOfobObjectIdenticalTo:p.view]添加到andControlTag中,可以获得所触视图数组中的位置,以便在“我的模式”对话框中传递该位置,以显示适当的信息