我的app是一个很普通的tabbar里嵌套navigationController的结构,里面要实现一个类似微信语音聊天的界面,那么我做了一个简易的底部栏用来放键盘输入和语音输入切换。
在我实现语音按钮输入的时候,我给button添加了不同touchEvent的回调
[longPressButton addTarget:self action:@selector(buttonTouchDown) forControlEvents:UIControlEventTouchDown];
[longPressButton addTarget:self action:@selector(buttonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
...
等几个event,用来显示不同状态下的语音提示。
现在问题就是button的UIControlEventTouchDown事件在手指按上去时不会触发,而当手指稍微滑动一下,才会触发。或者手指很快触碰然后拿开,会看到touchDown 和touchUpInside都触发了。
我一开始以为是和这个帖子一个问题:
http://www.cocoachina.com/bbs/read.php?tid=142527
但试过也没有解决。应该不是5楼说的问题,我就做了个小demo,发现3.5-inch屏幕没有问题,4-inch屏幕会出现问题,而且肯定是和tabbar有关系。
我把低栏的position移到中间一点,就不会出现上面的问题,把低栏放在tabbar的位置,就会出现。
而且我换过自己重写touchBegin方法,结果是和event事件一样不会触发。
圈子里一定有很多朋友做过类似的IM功能,所以希望能指点一二,我把例子代码放到附件了。
update:
我用reveal打开运行时的view hierarchy,发现底部的button没有被任何层遮住,还有一个现象很奇怪,仔细观察能发现,代码例子中在底部的button,touch处于屏幕左半部分是会有上述的问题的,touchBegin不响应,需要移动才响应,但button在屏幕的右半部分touch是ok的,就是会有些延迟,希望高手能帮我解决这个问题。
The first problem I solved after joining the company where the subject is working is this bug. For details, please see that placing the UIButton in the tabbar position under iOS 8 cannot respond to events.
I don’t have exact evidence, but based on your description, I guess it is.
The click area of the Tabbar is larger than the visible area, at least about 10 pixels higher. As shown in the picture, the green area is the click area, which is obviously higher than the visible area.
The click event of Cocoa is passed from bottom to top, so if your Tabbar is at the bottom, it must intercept the click first.
As for the solution, I think there are two ways. One is to make your Bar bigger, so big that it is still easy to click even if it is covered by the Tabbar by 10 pixels; the other is to rewrite the Tabbar.
Could it be that the thread is blocked? When touchUpInside, it returns to the main thread, so it responds to the TouchDown event that has been blocked before. Therefore, it does not respond at first, but then responds to two actions. If so If possible, check to see if there is a problem with multi-threading.
Hello, I downloaded your demo code on git. However, if you enter both sides of the tab and click the button, Log:
buttonTouchDown
will appear. Can you describe what effect you expect? Reproduce on which version and which screen size?Your pile of events can be regarded as a gesture, and your pressing and raising are all components of the gesture. Missing actions will lead to incomplete gesture judgment, making it impossible to trigger an event.
What you need is to override the callback method below
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
In addition, the problem of insensitive triggering may be that the triggering range is too small and the level value of the touch area cannot reach the threshold. After all, the standard requires an area of 44 pixels.
Please tell me how to solve it later. I encountered this problem at night. . . It seems that none of the above has any effect