我有一堆常规的 UIButton,按下时有一个目标。但我也希望它在滑动时工作,并激活 touchesbegan 或 touchesmoved/touchesended。我需要它,因为我想创建一个类似可滑动的单词拼图的东西,它也可以接受修饰印刷。但是滑动系列仅在触摸开始时才有效 self.view ,而不是按钮本身。它只会被按下并且不会注册任何触摸事件。
UIButton* Butt = [UIButton buttonWithType:UIButtonTypeCustom];
Butt.frame= CGRectMake(0, -500, 63 , 63);
[Butt addTarget:self action:@selector(clickWord:)forControlEvents:UIControlEventTouchUpInside];
Butt.userInteractionEnabled= YES;
[aSubview addSubview:Butt];
任何人都可以帮忙吗?我知道将 UIButtons 放在子视图中可能会很麻烦,但我真的希望这样做以使元素保持结构化,而不是所有内容都直接放在 self.view 上。
我试图对按钮进行子类化,但没有帮助
子类代码:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
// This is where the touch is passed on
}
- (void) toouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesMoved:touches withEvent:event];
[self.nextResponder touchesMoved:touches withEvent:event];
// This is where the touch is passed on
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self.nextResponder touchesEnded:touches withEvent:event];
// This is where the touch is passed on
}