Objective c 如何拖动和移动UITextFields?
我已将其设置为可以移动多个标签的位置。我在移动UITextfield时遇到问题。我确保启用了用户交互和多点触摸Objective c 如何拖动和移动UITextFields?,objective-c,ios4,ios5,Objective C,Ios4,Ios5,我已将其设置为可以移动多个标签的位置。我在移动UITextfield时遇到问题。我确保启用了用户交互和多点触摸 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint location = [touch locationInView:self.view]; if ([touch view] == t
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if ([touch view] == tex1) {
tex1.center = location;
试着做这个把戏,它对我有用
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *clearView = [[UIView alloc] initWithFrame:tex1.frame];
[clearView setTag:101];
[self.view addSubview:clearView];
[clearView release];
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([[touch view] tag] == 101 && [touch tapCount] == 2) {
[tex1 becomeFirstResponder];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if ([[touch view] tag] == 101) {
tex1.center = location;
[[touch view] setCenter:location];
}
}
试着做这个把戏,它对我有用
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *clearView = [[UIView alloc] initWithFrame:tex1.frame];
[clearView setTag:101];
[self.view addSubview:clearView];
[clearView release];
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([[touch view] tag] == 101 && [touch tapCount] == 2) {
[tex1 becomeFirstResponder];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if ([[touch view] tag] == 101) {
tex1.center = location;
[[touch view] setCenter:location];
}
}
您是在哪个类中定义此方法的?我不太确定,但我认为问题在于UILable view不与触摸交互,因此它会将事件向上传递到视图的层次结构,因此在viewController中,您可以通过触摸开始并移动标签来获取它。但是UITextView会耗尽touchEvent,例如,它会弹出键盘,因此touchEvent不会传递给超级用户。因此,在您的案例中,touchesBegind方法根本不会被调用。您在哪个类中定义此方法?我不太确定,但我认为问题在于可创建视图不会与touch交互,因此它会将事件向上传递到视图的层次结构,因此,在viewController中,您可以通过触摸开始并移动标签来获取它。但是UITextView会耗尽touchEvent,例如,它会弹出键盘,因此touchEvent不会传递给超级用户。因此,在您的情况下,touchesbeent方法根本不会被调用。让它工作吧!当我双击textfield时,有没有办法调用键盘。我想点击移动,双击编辑。@TWcode,是的,只需选中[touch tapCount]==2,查看我的更新答案快速问题!如何添加另一个textfield?每个textfield都有自己的框架,再创建一个tex2,再创建一个clearView2(标记=102),并复制相应的代码。让它工作吧!当我双击textfield时,有没有办法调用键盘。我想点击移动,双击编辑。@TWcode,是的,只需选中[touch tapCount]==2,查看我的更新答案快速问题!如何添加另一个textfield?每个textfield都有自己的框架,创建一个或多个tex2,创建一个或多个clearView2(标记=102),并复制相应的代码。