如何在 UIView 上创建可移动标签?

发布于 2024-12-11 10:37:33 字数 2160 浏览 0 评论 0原文

我正在尝试在 UIView 上创建四个可触摸和可移动的标签。

当我触摸 ifAnimaPart01 时,它们都会以某种方式聚集在一个位置,并且我无法触摸或移动其他标签。

有谁知道为什么我的四个标签这样做?

我添加了另外 4 个标签来显示 x 和 y 坐标以进行调试。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{   


UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];

    if ([touch view] == ifAnimPart01){
        ifAnimPart01.center = location;
    }   
    else if ([touch view] == ifAnimPart02){
        ifAnimPart02.center= location;
    }   
    else if ([touch view] == ifAnimPart03){
        ifAnimPart03.center= location;
    }
    else if ([touch view] == ifAnimPart04){
        ifAnimPart04.center= location;
    }

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [self touchesBegan:touches withEvent:event];

    UITouch *myTouch = [touches anyObject];
    startPoint1 = [myTouch locationInView:self.view];
    xCoordLabel1.text = [NSString stringWithFormat:@"Line OneX = %f", startPoint1.x];
    yCoordLabel1.text = [NSString stringWithFormat:@"Line OneY = %f", startPoint1.y];
    ifAnimPart01.center = CGPointMake(startPoint1.x, startPoint1.y);

    startPoint2 = [myTouch locationInView:self.view];
    xCoordLabel2.text = [NSString stringWithFormat:@"Line TwoX = %f", startPoint2.x];
    yCoordLabel2.text = [NSString stringWithFormat:@"Line TwoY = %f", startPoint2.y];
    ifAnimPart02.center = CGPointMake(startPoint2.x, startPoint2.y);

    startPoint3 = [myTouch locationInView:self.view];
    xCoordLabel3.text = [NSString stringWithFormat:@"Line ThreeX = %f", startPoint3.x];
    yCoordLabel3.text = [NSString stringWithFormat:@"Line ThreeY = %f", startPoint3.y];
    ifAnimPart03.center = CGPointMake(startPoint3.x, startPoint3.y);

    startPoint4 = [myTouch locationInView:self.view];
    xCoordLabel4.text = [NSString stringWithFormat:@"Line FourX = %f", startPoint4.x];
    yCoordLabel4.text = [NSString stringWithFormat:@"Line FourY = %f", startPoint4.y];
    ifAnimPart04.center = CGPointMake(startPoint4.x, startPoint4.y);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

}

I am trying to create four touchable and movable labels on a UIView.

When I touch a ifAnimaPart01 somehow they are all gathered in one location and I can not touch or move the other labels.

Does anyone know why my four labels are acting this way?

I added another 4 labels to display the x and y coordinates for debugging.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{   


UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];

    if ([touch view] == ifAnimPart01){
        ifAnimPart01.center = location;
    }   
    else if ([touch view] == ifAnimPart02){
        ifAnimPart02.center= location;
    }   
    else if ([touch view] == ifAnimPart03){
        ifAnimPart03.center= location;
    }
    else if ([touch view] == ifAnimPart04){
        ifAnimPart04.center= location;
    }

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [self touchesBegan:touches withEvent:event];

    UITouch *myTouch = [touches anyObject];
    startPoint1 = [myTouch locationInView:self.view];
    xCoordLabel1.text = [NSString stringWithFormat:@"Line OneX = %f", startPoint1.x];
    yCoordLabel1.text = [NSString stringWithFormat:@"Line OneY = %f", startPoint1.y];
    ifAnimPart01.center = CGPointMake(startPoint1.x, startPoint1.y);

    startPoint2 = [myTouch locationInView:self.view];
    xCoordLabel2.text = [NSString stringWithFormat:@"Line TwoX = %f", startPoint2.x];
    yCoordLabel2.text = [NSString stringWithFormat:@"Line TwoY = %f", startPoint2.y];
    ifAnimPart02.center = CGPointMake(startPoint2.x, startPoint2.y);

    startPoint3 = [myTouch locationInView:self.view];
    xCoordLabel3.text = [NSString stringWithFormat:@"Line ThreeX = %f", startPoint3.x];
    yCoordLabel3.text = [NSString stringWithFormat:@"Line ThreeY = %f", startPoint3.y];
    ifAnimPart03.center = CGPointMake(startPoint3.x, startPoint3.y);

    startPoint4 = [myTouch locationInView:self.view];
    xCoordLabel4.text = [NSString stringWithFormat:@"Line FourX = %f", startPoint4.x];
    yCoordLabel4.text = [NSString stringWithFormat:@"Line FourY = %f", startPoint4.y];
    ifAnimPart04.center = CGPointMake(startPoint4.x, startPoint4.y);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文