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

发布于 2024-12-05 05:40:05 字数 710 浏览 1 评论 0原文

请原谅我,我对此还很陌生。

我正在尝试检测类似于 MoveMe 示例的触摸 - 只是我将 UIViews (studentCell) 数组放入名为 StudentCellArray 的 NSMutableArray 中。

[self.studentCellArray addObject:self.studentCell];

当我进行一次触摸时,我想让程序足够智能,知道它是否触摸了数组中的任何 UIView,以及是否需要执行某些操作。

这是 TouchBegan: 方法中的代码。

//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));
}

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

Forgive 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2024-12-12 05:40:05

我通过为数组中的每个 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];
            }

这是我使用的“拖动”方法。我已将屏幕分为三部分,并且如果 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];
}

编辑

:将 [studentCellArray indexOfObjectIdenticalTo:p.view] 添加到 andControlTag 为我提供了所触摸的视图数组中的位置,以便我可以将其传递到我的模式对话框上以呈现适当的信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文