开发者

Detecting Touch of a UIView in UIScrollView layer with an array of UIViews

开发者 https://www.devze.com 2023-04-05 17:47 出处:网络
Fo开发者_Go百科rgive me I am kinda new at this. I am trying to detect a touch like the MoveMe example -- only I have an array of UIViews (studentCell) put in to a NSMutableArray called studentCellAr

Fo开发者_Go百科rgive me I am kinda new at this.

I am trying to detect a touch like the MoveMe example -- only I have an array of UIViews (studentCell) put in to a NSMutableArray called studentCellArray.

[self.studentCellArray addObject:self.studentCell];

When I have one touch I want to make the program smart enough to know if it has touched any of the UIViews in the array and if it has then to do something.

Here is the code in touchesBegan: 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));
}

I don't know how t access the views and touch them.


I "solved" this issue by assigning a UIPanGestureRecognizer to each UIView in the array.

This propbably isn't the best way to do it but I can now move them on screen.

Here is the code:

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

Here is the "dragging" method I used. I have divided the screen into thirds and there is an animation to snap the UIViews to a point if it happens to cross a threshold. I hope this gives someone some good ideas. Please help if you can see any better way to do this.

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

}

EDIT: Adding [studentCellArray indexOfObjectIdenticalTo:p.view] to the andControlTag gives me the position in the array of the view touched so i can pass that on the my modal dialog to present the appropriate information.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号