我有一个UILabels的NSMutableArray。 我需要能够在用户触摸时在此NSMutableArray中选择特定的UILabel,并将此触摸UILabel的中心移动到用户拖动手指的位置。
我可以通过执行以下操作在我的bunchOfLabels 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 to。
我是初学者,有人可以帮我解决这个问题吗?谢谢!
添加信息: 我目前正在使用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;
}
到目前为止一切正常,但我坚持能够在bunchOfLabels中移动特定的UILabel(在上面的代码中,objectAtIndex:1)。
答案 0 :(得分:3)
乌拉!!!得到它了! 我如下所示制作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];
}
感谢那些试图回答我的问题的人!