如果触摸移出 CGPoint,则取消触摸



我的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

最新更新