我的UIViewController
中心有400x400 RootController
(我们称之为ViewB)。在ViewB
内,我有少量UIButton
(自定义UIButton
类,触摸UIResponder
方法)。
我可以移动按钮,但当触摸屏离开ViewB时,按钮会取消触摸!
我真正想要的是完全取消触摸并将按钮靠近ViewB
的边缘。
答案 0 :(得分:2)
试试这个,
- (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