一个非常奇怪的结果。
- 启动单一 View 应用程序
- 添加一个 UILabel
把我的代码放进去
导入“ViewController.h”
@interface ViewController () { float oldX, oldY; bool dragging; __weak IBOutlet UILabel *textLable; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // self.textLable.backgroundColor = [UIColor clearColor]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // get touch event UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self.view]; if ([touch view] == textLable) { int intValue = (int)((touchLocation.y * 24.0)/300.0); NSString *anyValue = [NSString stringWithFormat:@"%d",intValue]; textLable.center= touchLocation; // RUN WITHOUT THIS PART .... tochedMoved workt great // // Run With this PART ..... touchedMoved is damaged!!!!! //textLable.text = anyValue; NSLog(@"%f %f", touchLocation.y, textLable.frame.origin.y); } } @end
用
连接标签__weak IBOutlet UILabel *textLable;
让它运行
现在您可以通过触摸 move 来 move 标签。
好的...现在!!!!
改变改变
// RUN WITHOUT THIS PART .... tochedMoved workt great
//
// Run With this PART ..... touchedMoved is damaged!!!!!
//textLable.text = anyValue;
到
// RUN WITHOUT THIS PART .... tochedMoved workt great
//
// Run With this PART ..... touchedMoved is damaged!!!!!
textLable.text = anyValue; //<------------------------------
运行应用程序并尝试 move !!!如果您开始触摸 move ,您会看到标签在新位置和起始位置之间跳转。
我尝试使用 UIView 作为容器......同样的事情:一旦你改变了 move 对象的值(UIButton 相同), move 就无法正常工作。
向 Appel 的报告已经发送...没有回答!!!
是 Bug 还是功能???
最佳答案
这个想法是关于你的框架和你的位置触摸。您正在将 Label 置于您的触摸中心。所以没有什么是关于设置文本的。 试试这段代码,在第一次触摸时检测触摸和中心文本之间的距离,并将中心标签添加到这一点:
@interface ViewController ()
{
float distanceTouchX, distanceTouchY;
bool dragging;
CGPoint firstLocation;
__weak IBOutlet UILabel *textLable;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
distanceTouchX = textLable.center.x - touchLocation.x;
distanceTouchY = textLable.center.y - touchLocation.y;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == textLable)
{
int intValue = (int)((touchLocation.y * 24.0)/320.0);
NSString *anyValue = [NSString stringWithFormat:@"%d",intValue];
CGPoint newTouchLocation = touchLocation;
newTouchLocation.x = newTouchLocation.x + distanceTouchX;
newTouchLocation.y = newTouchLocation.y + distanceTouchY;
textLable.center= newTouchLocation;
textLable.text = anyValue;
NSLog(@"%f %f", touchLocation.y, textLable.frame.origin.y);
}
}
@end
关于ios - 触摸 move UILabel 一直有效,直到值修复 - 错误还是功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28085197/