Обнаружение касания UIView в слое UIScrollView с массивом UIViews - PullRequest
0 голосов
/ 16 сентября 2011

Простите, я новичок в этом.

Я пытаюсь обнаружить касание, как в примере MoveMe - только у меня есть массив UIViews (studentCell), вставленный в NSMutableArray с именем studentCellArray.

[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 Ответ

1 голос
/ 16 сентября 2011

Я «решил» эту проблему, назначив 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 дает мне положение в массивевида, которого коснулись, чтобы я мог передать это в моем модальном диалоговом окне, чтобы представить соответствующую информацию.

...