我有一个NSMutableArray of uilabel。我需要能够在这个NSMutableArray中选择一个特定的用户触摸UILabel,并将这个触摸UILabel的中心移动到用户拖动手指的位置。
我能够移动一个特定的UILabel在我的bunchOfLabels NSMutableArray通过这样做:
UIGestureRecognizer *gestureRecognizer;
touchPosition = [gestureRecognizer locationInView:mainView];
NSLog(@"x: %f", touchPosition.x);
UILabel *temp;
temp = [bunchOfLabels objectAtIndex:0];
temp.center = touchPosition;
this将始终移动第一个标签,即使用户触及第二个,第三个或任何标签。
但我需要能够说,移动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;
}
到目前为止一切都很好,但我一直坚持能够在bunchOfLabels中移动特定的UILabel(在上面的代码中,objectAtIndex:1)。
万岁!!得到它!我使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];
}
感谢那些试图回答我问题的人!