gpt4 book ai didi

ios touch 从一个按钮移动到另一个按钮

转载 作者:行者123 更新时间:2023-11-29 11:01:40 29 4
gpt4 key购买 nike

我有两个自定义 uibutton。

@interface myButton : UIButton

我覆盖了几个方法,比如

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// do something
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
[self.nextResponder touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];

if (!CGRectContainsPoint(self.bounds, touchPoint)) {
[self touchesCancelled:touches withEvent:event];
}
return;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// do something
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
return;
}

我想要的是,当我触摸一个按钮时,我可以继续将我的触摸移动到另一个按钮。当触摸超出我触摸的第一个按钮的范围时,我厌倦了调用 touchesCancelled。所以我“认为”然后我移动到另一个按钮,这将是一个新的触摸事件。但它不是这样工作的。

我做错了什么吗?或者 touchesCancelled 方法不用于此目的?提前致谢!

最佳答案

您的怀疑是正确的,这不是 touchesCancelled:withEvent: 的用途。来自文档:

This method is invoked when the Cocoa Touch framework receives a system interruption requiring cancellation of the touch event; for this, it generates a UITouch object with a phase of UITouchPhaseCancel. The interruption is something that might cause the application to be no longer active or the view to be removed from the window.

如果您的用户收到来电、短信或他们的闹钟响起等,您的响应者将收到触摸取消事件。它应该用于清除在其他触摸事件中建立的任何状态信息。

似乎您想在触摸中更改与触摸事件关联的响应者,即当您从第一个按钮的边界拖动触摸并输入第二个按钮的触摸时,它应该成为接收触摸事件的响应者.不幸的是,这不是响应者设计的工作方式。一旦 UIView 被识别为响应者,如 hitTest:withEvent: 中返回的那样,这将是接收触摸事件的 UIView

实现你想要的可能的锻炼是在包含你的两个按钮。然后你的 super View 将收到触摸事件,你可以根据你在哪个按钮的框架内采取行动:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
CGPoint touchLocation = [touch locationInView:self.view];

UIButton *button;
if (CGRectContainsPoint(self.button1.frame, touchLocation))
{
button = self.button1;
NSLog(@"touchesMoved: First Button");
}
else if (CGRectContainsPoint(self.button2.frame, touchLocation))
{
button = self.button2;
NSLog(@"touchesMoved: Second Button");
}

// Do something with button...
}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
CGPoint touchLocation = [touch locationInView:self.view];

UIButton *button;
if (CGRectContainsPoint(self.button1.frame, touchLocation))
{
button = self.button1;
NSLog(@"touchesMoved: First Button");
}
else if (CGRectContainsPoint(self.button2.frame, touchLocation))
{
button = self.button2;
NSLog(@"touchesMoved: Second Button");
}

// Do something with button...
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
CGPoint touchLocation = [touch locationInView:self.view];

UIButton *button;
if (CGRectContainsPoint(self.button1.frame, touchLocation))
{
button = self.button1;
NSLog(@"touchesEnded: First Button");
}
else if (CGRectContainsPoint(self.button2.frame, touchLocation))
{
button = self.button2;
NSLog(@"touchesEnded: Second Button");
}

// Do something with button...
}
}

关于ios touch 从一个按钮移动到另一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15640981/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com