UIButton 在 uitouch 事件上崩溃

发布于 2024-10-21 06:48:57 字数 690 浏览 4 评论 0原文

我有一个 UIButton,它将 Draginside 事件发送到:

-(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];
}

I have a UIButton which sends the draginside event to:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

It does this through the following code in viewDidLoad:

[colourButton1 addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];

Within the method it sends it to, there is the following line:

UITouch *myTouch = [touches anyObject];

And for some reason, when dragging inside the UIButton this crashes the app. Any ideas why?

EDIT: The solution..

-(IBAction)buttonDragged:(id)button withEvent:(UIEvent*)event {
    NSSet *touches = [event touchesForView:colourButton1];
    [self touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无戏配角 2024-10-28 06:48:57

当您向控件添加目标时,您可以传递 3 种类型的操作选择器 -

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender withEvent:(UIEvent*)event

名称并不重要,重要的是参数的数量。如果控件向其目标发送带有 2 个参数的消息,则第一个参数将是控件本身(在您的情况下是 UIButton 的实例),第二个参数是 UIEvent 的实例。但是您希望将 NSSet 实例作为第一个参数,并向其发送 anyObject 消息,而 UIButton 无法理解。这就是崩溃的原因。

您为什么首先尝试将事件从 UI 控件发送到触摸处理方法 touchesMoved: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];
}

请注意,由于 touchesMoved:withEvent:UIResponder 的方法,而控制器的视图 UIResponder 此方法也会在此视图的触摸事件上调用。

When you're adding target to control you can pass 3 types of selectors for action -

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender withEvent:(UIEvent*)event

Names don't matter, it's number of parameter which is important. If control sends it's target a message with 2 params then first parameter will be control itself (instance of UIButton in your case), second one - instance of UIEvent. But you expects instance of NSSet as first parameter and send it a message anyObject which UIButton doesn't understand. This is the cause of crash.

Why are you trying to send event from UI control to touch handling method touchesMoved:withEvent: in the first place? It will probably do something different from what you meant.

UPDATE:

- (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];
}

Notice that since touchesMoved:withEvent: is a UIResponder's method and controller's view is UIResponder this method will be also called on touch events for this view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文