如何在 UILabels 的 NSMutableArray 中移动 UILabel 的中心,取决于用户触摸该 UILabel 并将其拖动到的位置?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
欢呼!!!知道了!
我如下制作 panElement 并且现在可以使用了!
感谢那些试图回答我的问题的人!
Hurray!!! Got it!
I make panElement as below and it works now!
Thanks to those attempting to answer my question!