我正在制作一个自定义 iOS 键盘,并有一个 UIControl 子类来表示我的按钮。我试图获得与普通 iOS 键盘相同的行为:
我正在使用这样的触摸跟踪方法进行测试:
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
[super beginTrackingWithTouch:touch withEvent:event];
NSLog(@"Begin for %@", [self label]);
return YES;
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
[super continueTrackingWithTouch:touch withEvent:event];
NSLog(@"Continue for %@", [self label]);
return YES;
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
[super endTrackingWithTouch:touch withEvent:event];
NSLog(@"End for %@", [self label]);
}
这些方法都被调用,除了它们只在触摸开始的 UIControl 上被调用。
识别在我的所有按钮上来来往往的触摸的最佳方式是什么?我必须通过父 View 完成所有操作吗?
最佳答案
如果提供,我会选择一个更好的答案......但如果有人通过搜索找到这个,我已经设法通过这种方式得到我需要的东西:
userInteractionEnabled
至NO
对于按钮类(UIControl 子类)touchesBegan:withEvent:
, touchesMoved:withEvent:
和 touchesEnded:withEvent:
在 View Controller 中 - (MyButton *)buttonForTouch:(UITouch *)touch
{
CGPoint windowLocation = [touch locationInView:keyboardView];
for (MyButton *button in buttons) {
if (CGRectContainsPoint([button frame], windowLocation)) {
return button;
}
}
return nil;
}
touchesBegan:withEvent:
中保留对 UITouch 实例的引用。所以你可以确定你在其他方法中跟踪的是同一个 关于ios - UIControl 跟踪在不同 UIControl 上开始的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26190857/