我正在Objective-c中开发一个非常简单的应用程序.
在应用程序中,用户可以通过拖动屏幕来更改标签的位置.
点击并拖动屏幕,标签上下移动以匹配手指的位置.
松开手指时,标签的坐标信息将设置为其文本.
但标签位置在其文本更改时重置.
// IBOutlet UILabel *label
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touch = [[touches anyObject] locationInView:self.view];
label.center = CGPointMake(label.center.x, touch.y);
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touch = [[touches anyObject] locationInView:self.view];
label.center = CGPointMake(label.center.x, touch.y);
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touch = [[touches anyObject] locationInView:self.view];
label.text = [NSString stringWithFormat:@"y = %f", touch.y];
}
Run Code Online (Sandbox Code Playgroud)
在touchesEnded事件中,只需更改标签的文本,
但其位置已重置.
我试图改变touchesEnded事件如下,但它没有解决问题.
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{ …Run Code Online (Sandbox Code Playgroud)