2013-02-01 53 views
0

我想更改標籤的文本,然後讓用戶將其移動到屏幕上的當前正在工作的位置(用戶點擊 - 「添加文本」)。更改uilabel的文本,然後添加新的uilabel

一旦他們把它放在他們想要的地方。我想要「添加文本」按鈕來創建一個用戶可以移動的新標籤。我不確定如何動態創建這些動作,以確保手勢識別器與新標籤一起工作。感謝您的建議。

這就是我現在的,,,還沒有工作。


-(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); 
} 
+0

添加您使用的代碼添加並移動您的第一個標籤,請! – Moxy

+0

我在界面構建器中添加了第一個標籤。第二次標籤甚至可以被光柵化並且如果更容易就不可移動。 –

+0

和手勢識別器?也在界面生成器? – Moxy

回答

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); 
} 

我不知道這是否足以解決你的問題,只是評論,如果您還需要更多的解釋。

+0

用上面的代碼更新了......沒有任何東西跳出來嗎? –

+0

好的......我現在正在讀這篇文章,我想我需要添加一些東西來讓手勢起作用 –

+0

試過這個......我想我需要添加手勢識別器,正確嗎?UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizer頁頭] initWithTarget:自 行動:@選擇(labelMoved :)]; panGestureRecognizer.delegate =自我; [查看addGestureRecognizer:panGestureRecognizer]; –