2011-09-15 55 views
0

原諒我,我在這方面有點新鮮。用UIViews數組檢測UIScrollView圖層中UIView的觸摸

我想檢測一個像MoveMe例子的觸摸 - 只有我有一個UIViews(studentCell)數組放入一個名爲studentCellArray的NSMutableArray。

[self.studentCellArray addObject:self.studentCell]; 

當我有一個觸摸我想使程序足夠聰明,知道它已經觸及任何UIViews陣列中,如果有則做一些事情。

這裏是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)); 
} 

我不知道如何訪問視圖並觸摸它們。

回答

1

我通過給數組中的每個UIView分配一個UIPanGestureRecognizer來「解決」了這個問題。

這可能不是最好的辦法,但我現在可以在屏幕上移動它們。

下面是代碼:

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給我的視圖的陣列中的位置被觸摸,所以我可以傳遞關於我的模態對話框以呈現適當的信息。