0

我想更改标签的文本,然后让用户将其移动到屏幕上他们想要的位置(当前正在工作)(用户点击 - “添加文本”)。

一旦他们把它放在他们想要的地方。我希望“添加文本”按钮创建一个用户可以移动的新标签。我不确定如何动态创建这些以确保手势识别器与新标签一起使用。感谢您的建议。

这就是我现在所拥有的,,,还没有工作。


-(IBAction)addText:(id)sender
{
    textView.hidden=YES;


    labelShirt.text= textField.text;
    [textField resignFirstResponder];

    [self addTextButtonPressed];

}



-(void)addTextButtonPressed
{
// CGRect *textFrame =
    // myInitialFrame is a CGRect you choose to place your label
    UILabel *myNewLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,50,100,100)];
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                           action:@selector(labelMoved:)];
   myNewLabel.text =textField.text;

[self.view addSubview:myNewLabel];
}

-(void)labelMoved:(UIPanGestureRecognizer *)sender
{
    CGPoint translation = [sender translationInView:self.view];
    sender.view.frame = CGRectOffset(sender.view.frame, translation.x, translation.y);
}
4

1 回答 1

1
// The action that is added to your add text button
-(void)addTextButtonPressed
{
    // myInitialFrame is a CGRect you choose to place your label
    UILabel *myNewLabel = [[UILabel alloc] initWithFrame:myInitialFrame];
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                           action:@selector(labelMoved:)];
    myNewLabel.text = @"My initial text";
    // EDIT
    [self.view addSubview:myNewLabel];
    [myNewLabel addGestureRecognizer:panGestureRecognizer];
}

-(void)labelMoved:(UIPanGestureRecognizer *)sender
{
    CGPoint translation = [sender translationInView:self.view];
    sender.view.frame = CGRectOffset(sender.view.frame, translation.x, translation.y);
}

我不知道这是否足以解决您的问题,如果您仍然需要更多解释,请发表评论。

于 2013-02-01T00:39:49.940 回答