我有一个链接到 iboutlet 的按钮。单击按钮 (*buttonlabel) 时,会生成一个标签。标签可以在屏幕上移动到任意位置。如果我再次单击该按钮,则会生成另一个标签,但我无法再移动第一个标签。我在互联网上搜索,发现我必须使用 NSMutablearray,但这似乎不起作用:
英寸米
-(IBAction)button3Pressed:(id)sender{
self.buttonBrush.selected = NO;
self.buttonBrush.highlighted = NO;
self.buttonlabel.selected = YES;
self.buttonlabel.highlighted = YES;
self.buttontextbox.selected = NO;
self.buttontextbox.highlighted = NO;
CGRect labelFrame = CGRectMake( 400, 100, 100, 30);
label1 = [[UILabel alloc] initWithFrame: labelFrame];
[label1 setText: @"Untitled"];
[label1 setTextColor: [UIColor orangeColor]];
[self.view addSubview:label1];
label1.backgroundColor = [UIColor clearColor];
if (array == nil) array = [[NSMutableArray alloc]init];
[array addObject:label1];
}
然后
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//label1
if (buttonlabel.selected == YES){
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
UIView *touchedView = [touch view];
if ([array containsObject:touchedView]){
touchedView.center = location;
}
}
}
在 .h 中我添加了:
IBOutlet UILabel *label1;
NSMutableArray *array;
提前感谢您的帮助
最佳答案
编辑:使用它来测试数组中的所有标签:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
for (UILabel *label in array) {
CGPoint localLocation = [label convertPoint:location fromView:self.view];
if ([label pointInside:localLocation withEvent:nil]) {
label.center = location;
break;
}
}
}
关于ios - 使用 NSMutablearray 在屏幕上移动 UILabels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9877668/