ios - 使用 NSMutablearray 在屏幕上移动 UILabels

标签 ios xcode

我有一个链接到 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/

相关文章:

ios - XEP-0363 : HTTP File Upload in Swift

android - 使网站适合移动设备的最佳方法是什么?

iphone - 在同一 View Controller 中具有 TableView 的 ImageView

swift - 错误 ITMS-90205 : "Invalid Bundle. The bundle at ' NotificationServiceExtension. appex' 包含不允许的嵌套包。”

ios - 为什么不能将 recordAudioURL 分配给 segue.destination 对象?

objective-c - 将触摸传递到 UIWebView 的一部分

ios - 在 iOS 6 上第二次未设置值。线程问题?

ios - Xcode 6 GM iPhone 6 模拟器 - Storyboard大小不正确,放大显示

iphone - 我有一个在 MAC OS 上使用 XCode 开发的项目,我如何在 Linux 上构建它?

ios - 选择日期后如何禁用 UIDatePicker?