如果移出CGPoint,则取消触摸

时间:2014-02-13 09:59:18

标签: ios objective-c ipad uibutton

我的UIViewController中心有400x400 RootController(我们称之为ViewB)。在ViewB内,我有少量UIButton(自定义UIButton类,触摸UIResponder方法)。

我可以移动按钮,但当触摸屏离开ViewB时,按钮会取消触摸!

我真正想要的是完全取消触摸并将按钮靠近ViewB的边缘。

1 个答案:

答案 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

的子视图