gpt4 book ai didi

ios - 如果移出 CGPoint 则取消触摸

转载 作者:行者123 更新时间:2023-11-28 22:15:46 25 4
gpt4 key购买 nike

我的 RootController 中心有一个 400x400 UIViewController(我们称它为 ViewB)。在 ViewB 中,我有几个 UIButton(自定义 UIButton 类,带有触摸 UIResponder 方法)。

我可以四处移动按钮,但是当触摸离开 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 的 subview

关于ios - 如果移出 CGPoint 则取消触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21750832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com