如何在 UILabels 的 NSMutableArray 中移动 UILabel 的中心,取决于用户触摸该 UILabel 并将其拖动到的位置?

发布于 2024-12-02 08:24:42 字数 1763 浏览 0 评论 0原文

我有一个 UILabels 的 NSMutableArray。 我需要能够在用户触摸时选择此 NSMutableArray 中的特定 UILabel,并将此触摸 UILabel 的中心移动到用户将手指拖动到的位置。

我可以通过执行以下操作来移动一堆标签 NSMutableArray 中的特定 UILabel:

UIGestureRecognizer *gestureRecognizer;
touchPosition = [gestureRecognizer locationInView:mainView];
NSLog(@"x: %f", touchPosition.x);

UILabel *temp;
temp = [bunchOfLabels objectAtIndex:0];
temp.center = touchPosition;

这将始终移动第一个标签,即使用户触摸第二个、第三个或任何标签。

但我需要能够说,将 objectAtIndex:4 UILabel 移动到用户触摸并拖动 objectAtIndex:4 UILabel 的任何位置。

我是初学者,有人可以帮我解决这个问题吗?谢谢!

添加信息: 我目前正在使用 UIPanGestureRecognizer,如下所示:

-(void)setupLabels {

    bunchOfLabels = [[NSMutableArray alloc] initWithCapacity:[characters count]];

    for (int i=0; i < [characters count]; i++)  {

        int xPosition = arc4random() % 518;
        int yPosition = arc4random() % 934;

        UILabel *tempCharacterLabel = [[UILabel alloc] initWithFrame:CGRectMake(xPosition, yPosition, 60, 60)];
        tempCharacterLabel.text = [characters objectAtIndex:i]; // characters is another NSMutableArray contains of NSStrings
        [tempCharacterLabel setUserInteractionEnabled:YES];
        [bunchOfLabels addObject:tempCharacterLabel];

        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panElement:)];
        [panGesture setMaximumNumberOfTouches:2];
        [[bunchOfLabels objectAtIndex:i] addGestureRecognizer:panGesture];

    }
}

-(void)panElement:(UIPanGestureRecognizer *)gestureRecognizer 
{
    UILabel *temp;
    temp = [bunchOfLabels objectAtIndex:1];
    temp.center = touchPosition;
}

到目前为止,一切都很好,但我一直坚持能够在 chunkOfLabels 中移动特定的 UILabel (在上面的代码中,objectAtIndex:1)。

I have a NSMutableArray of UILabels.
I need to be able to select a particular UILabel in this NSMutableArray upon a user touch, and move the center of this touch UILabel to where the user drags their finger to.

I am able to move a particular UILabel in my bunchOfLabels NSMutableArray by doing:

UIGestureRecognizer *gestureRecognizer;
touchPosition = [gestureRecognizer locationInView:mainView];
NSLog(@"x: %f", touchPosition.x);

UILabel *temp;
temp = [bunchOfLabels objectAtIndex:0];
temp.center = touchPosition;

this will always move the first label, even if the user touches 2nd, 3rd, or whatever label.

but I need to be able to say, move objectAtIndex:4 UILabel anywhere the user touches and drags on a objectAtIndex:4 UILabel to.

I'm a beginner, could somebody please help me out with this? Thanks!

Added info:
I'm currently using UIPanGestureRecognizer, like this:

-(void)setupLabels {

    bunchOfLabels = [[NSMutableArray alloc] initWithCapacity:[characters count]];

    for (int i=0; i < [characters count]; i++)  {

        int xPosition = arc4random() % 518;
        int yPosition = arc4random() % 934;

        UILabel *tempCharacterLabel = [[UILabel alloc] initWithFrame:CGRectMake(xPosition, yPosition, 60, 60)];
        tempCharacterLabel.text = [characters objectAtIndex:i]; // characters is another NSMutableArray contains of NSStrings
        [tempCharacterLabel setUserInteractionEnabled:YES];
        [bunchOfLabels addObject:tempCharacterLabel];

        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panElement:)];
        [panGesture setMaximumNumberOfTouches:2];
        [[bunchOfLabels objectAtIndex:i] addGestureRecognizer:panGesture];

    }
}

-(void)panElement:(UIPanGestureRecognizer *)gestureRecognizer 
{
    UILabel *temp;
    temp = [bunchOfLabels objectAtIndex:1];
    temp.center = touchPosition;
}

Everything is OK so far, but I've stuck at being able to move a particular UILabel in bunchOfLabels (in the code above, objectAtIndex:1).

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

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

发布评论

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

评论(1

漫漫岁月 2024-12-09 08:24:42

欢呼!!!知道了!
我如下制作 panElement 并且现在可以使用了!

-(void)panElement:(UIPanGestureRecognizer *)gesture
{
    UILabel *tempLabel = (UILabel *)gesture.view;
    CGPoint translation = [gesture translationInView:tempLabel];

    tempLabel.center = CGPointMake(tempLabel.center.x + translation.x, tempLabel.center.y + translation.y);

    [gesture setTranslation:CGPointZero inView:tempLabel];
}

感谢那些试图回答我的问题的人!

Hurray!!! Got it!
I make panElement as below and it works now!

-(void)panElement:(UIPanGestureRecognizer *)gesture
{
    UILabel *tempLabel = (UILabel *)gesture.view;
    CGPoint translation = [gesture translationInView:tempLabel];

    tempLabel.center = CGPointMake(tempLabel.center.x + translation.x, tempLabel.center.y + translation.y);

    [gesture setTranslation:CGPointZero inView:tempLabel];
}

Thanks to those attempting to answer my question!

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