我想通过触摸 UIBarButtonItem 并移动来创建滑动 UIToolbar。我阅读了一些主题,发现我必须实现 TouchMoved。但是哪里?我唯一发现它是 UIButton 的地方,我位于 UIBarButtonItem 中。
这是viewController viewDidLoad
self.toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(bounds.origin.x, bounds.size.height - 2.5*height, bounds.size.width, 2*height)];
SlideButton* infoButton = [SlideButton buttonWithType: UIButtonTypeDetailDisclosure];
UIBarButtonItem *slideButton = [[UIBarButtonItem alloc]initWithCustomView:infoButton];
self.toolbar.items = items;
[self.view addSubview:self.toolbar];
最奇怪的是,我尝试将 UIButton 绑定(bind)到操作(addTarget:action:forEvents) 而且它很有效,非常精确。但我无法获取触摸的位置,因为我没有发生任何事件,所以我不能使用这种方法。
最佳答案
您可以将 UIPanGestureRecognizer
添加到工具栏。
它仅识别平移手势(拖动)。在目标方法中,使用 CGRectContainsPoint(CGRect, point) 方法检查触摸是否在条形按钮的框架内,并像 if(UIGestureRecognizerStateBegan)
那样进行第一次触摸。
如果是,则在 UIGestureRecognizerStateEnded 或之后启用拖动
UIGestureRecognizerStateCancelled
禁用拖动。
关于ios - TouchMoved 不适用于 UIBarButtonItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15342223/