Iphone uitouch事件中UIButton崩溃
我有一个UIButton,它将draginside事件发送到:Iphone uitouch事件中UIButton崩溃,iphone,objective-c,uibutton,uitouch,Iphone,Objective C,Uibutton,Uitouch,我有一个UIButton,它将draginside事件发送到: -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 它通过viewDidLoad中的以下代码完成此操作: [colourButton1 addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown]; 在它发送到的方法
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
它通过viewDidLoad中的以下代码完成此操作:
[colourButton1 addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];
在它发送到的方法中,有以下行:
UITouch *myTouch = [touches anyObject];
由于某种原因,当拖动UIButton时,应用程序会崩溃。你知道为什么吗
编辑:解决方案
-(IBAction)buttonDragged:(id)button withEvent:(UIEvent*)event {
NSSet *touches = [event touchesForView:colourButton1];
[self touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event];
}
将目标添加到控件时,可以传递3种类型的选择器以执行操作-
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender withEvent:(UIEvent*)event
名称并不重要,重要的是参数的数量。如果控件向其目标发送带有2个参数的消息,则第一个参数将是控件本身(在您的情况下为UIButton
的实例),第二个参数是UIEvent
的实例。但您希望将NSSet
的实例作为第一个参数,并向其发送一条anyObject
消息,而UIButton
无法理解该消息。这就是撞车的原因。
首先,为什么要尝试将事件从UI控件发送到触摸处理方法touchsmoved:withEvent:
?它可能会做一些与你的意思不同的事情
更新:
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event {
UITouch *t = [touches anyObject];
CGPoint touchLocation = [t locationInView:self.view];
NSLog(@"%@", NSStringFromCGPoint(touchLocation));
}
- (IBAction)buttongDragged:(id)button withEvent:(UIEvent*)event {
NSSet *touches = [event touchesForView:button];
[self touchesMoved:touches withEvent:event];
}
请注意,由于
TouchsMoved:withEvent:
是一个UIResponder
的方法,而控制器的视图是UIResponder
,因此此视图的触摸事件也会调用此方法。当您将目标添加到控件时,可以传递3种类型的选择器进行操作-
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender withEvent:(UIEvent*)event
名称并不重要,重要的是参数的数量。如果控件向其目标发送带有2个参数的消息,则第一个参数将是控件本身(在您的情况下为UIButton
的实例),第二个参数是UIEvent
的实例。但您希望将NSSet
的实例作为第一个参数,并向其发送一条anyObject
消息,而UIButton
无法理解该消息。这就是撞车的原因。
首先,为什么要尝试将事件从UI控件发送到触摸处理方法touchsmoved:withEvent:
?它可能会做一些与你的意思不同的事情
更新:
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event {
UITouch *t = [touches anyObject];
CGPoint touchLocation = [t locationInView:self.view];
NSLog(@"%@", NSStringFromCGPoint(touchLocation));
}
- (IBAction)buttongDragged:(id)button withEvent:(UIEvent*)event {
NSSet *touches = [event touchesForView:button];
[self touchesMoved:touches withEvent:event];
}
请注意,由于
touchsmoved:withEvent:
是UIResponder
的方法,控制器的视图是UIResponder
,因此此视图的触摸事件也将调用此方法。,因为我希望用户能够将按钮拖动到其他位置。我在上面编辑了更多的代码。我明白了。我不知道这是否是实现这种行为的最佳方式,但如果您确实需要它,您可以从event-NSSet*touchs=[event-touchesForView:sender]
获得触动,您可以创建一个类似'-(iAction)buttonDragged:(id)button with event:(UIEvent*)event的方法,将目标添加到按钮,并将其作为操作,从上面的注释中用代码检索触摸事件,并调用
touchsmoved:withEvent:`直接用这个触摸。嗯,我仍然不理解这个。你能在上面的答案中编辑一个例子吗?没关系,我解决了。我将把你的答案编辑到我的问题中,供大家将来参考。非常感谢。因为我希望用户能够将按钮拖动到其他位置。我在上面编辑了更多的代码。我明白了。我不知道这是否是实现这种行为的最佳方式,但如果您确实需要它,您可以从event-NSSet*touchs=[event-touchesForView:sender]
获得触动,您可以创建一个类似'-(iAction)buttonDragged:(id)button with event:(UIEvent*)event的方法,将目标添加到按钮,并将其作为操作,从上面的注释中用代码检索触摸事件,并调用
touchsmoved:withEvent:`直接用这个触摸。嗯,我仍然不理解这个。你能在上面的答案中编辑一个例子吗?没关系,我解决了。我将把你的答案编辑到我的问题中,供大家将来参考。非常感谢。