我的RootController
中心有一个 400x400 UIViewController
(我们称之为 ViewB)。在ViewB
里面,我几乎没有UIButton
(带有触摸UIResponder
方法的自定义UIButton
类)。
我可以移动按钮,但是当触摸离开 ViewB 时,按钮确实会取消触摸!
我真正想要的是完全取消触摸并将按钮留在ViewB
边缘附近。
试试这个,
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
....
CGRect viewBFrame = ViewB.view.frame;
CGRect buttonToRect = //calculated button frame
CGPoint buttonOrigin = buttonToRect.origin;
CGFloat xMax = CGRectGetWidth(viewBFrame) - CGRectGetWidth(buttonToRect);
CGFloat yMax = CGRectGetHeight(viewBFrame) - CGRectGetHeight(buttonToRect);
buttonOrigin.x = MAX(0, buttonOrigin.x);
buttonOrigin.x = MIN(xMax, buttonOrigin.x);
buttonOrigin.y = MAX(0, buttonOrigin.y);
buttonOrigin.y = MIN(yMax, buttonOrigin.y);
//set buttonToRect to Button
}
注意:假设按钮是子视图ViewB