为了账号安全,请及时绑定邮箱和手机立即绑定

有没有办法在iPhone上通过触摸?

有没有办法在iPhone上通过触摸?

慕慕森 2019-12-26 11:00:43
UIButton在主区域中点击时,我使用几个s来设置当前动作。我还想允许用户从按钮直接拖动到主要区域并执行相同的操作;本质上,在触摸UIButton时应将touchesBegan和touchesMoved传递给主视图,还应发送按钮按下动作。现在,我可以更改控件了。拖动出口将调用内部触摸区域以设置控件,然后调用触摸开始区域以开始主区域触摸操作。但是,此时,touchesMoved和touchesEnded显然没有被调用,因为触摸起源于UIButton。有没有一种方法可以半忽略触摸,以便将它们传递到主区域,还可以让我先设置控件?
查看完整描述

3 回答

?
MMTTMM

TA贡献1869条经验 获得超4个赞

在文档中,查找“ 响应者对象”和“响应者链”


您可以通过转发响应者链上的修饰来“共享”对象之间的修饰。您的UIButton有一个接收UITouch事件的响应器/控制器,我的猜测是,一旦它对返回的消息执行了正确的解释-触摸已被处理和处置。


苹果公司建议这样的事情(当然取决于触摸的类型):


[self.nextResponder touchesBegan:touches withEvent:event];


而不是处理触摸事件,而是将其传递出去。


子类UIButton:


MyButton.h


#import <Foundation/Foundation.h>


@interface MyButton : UIButton {


}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event ;


@end

我的按钮


#import "MyButton.h"


@implementation MyButton


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

    printf("MyButton touch Began\n");

    [self.nextResponder touchesBegan:touches withEvent:event]; 

}

@end


查看完整回答
反对 回复 2019-12-26
?
喵喔喔

TA贡献1735条经验 获得超5个赞

我知道这个问题已经有两年了(并且已经回答了),但是...


当我自己尝试时,触摸被转发了,但是按钮不再像按钮那样表现。我也将修饰传递给“超级”,现在一切都很好。


因此,对于可能偶然发现此问题的初学者,代码应如下所示:


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

    [super touchesBegan:touches withEvent:event];

    [self.nextResponder touchesBegan:touches withEvent:event]; 

}


查看完整回答
反对 回复 2019-12-26
?
临摹微笑

TA贡献1982条经验 获得超2个赞

也无需继承!只需将其放在实现的顶层即可,然后再进行其他操作:


#pragma mark PassTouch


@interface UIButton (PassTouch)

@end


@implementation UIButton (PassTouch)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [super touchesBegan:touches withEvent:event];

    [self.nextResponder touchesBegan:touches withEvent:event];

}

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

{

    [super touchesMoved:touches withEvent:event];

    [self.nextResponder touchesMoved:touches withEvent:event];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    [super touchesEnded:touches withEvent:event];

    [self.nextResponder touchesEnded:touches withEvent:event];

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

    [super touchesCancelled:touches withEvent:event];

    [self.nextResponder touchesCancelled:touches withEvent:event];

}

@end


查看完整回答
反对 回复 2019-12-26
  • 3 回答
  • 0 关注
  • 646 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信