【问题标题】:Cancel touches if it moves out CGPoint如果移出 CGPoint 则取消触摸
【发布时间】:2014-02-13 09:59:18
【问题描述】:

我的RootController 的中心有一个 400x400 UIViewController(我们称之为 ViewB)。在ViewB 里面我很少有UIButton (自定义UIButton 类与触摸UIResponder 方法)。

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

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

【问题讨论】:

  • 你如何移动按钮?您可以将 MIN 和 MAX 点设置为按钮并将其限制在 ViewB 边界内。
  • 你需要检测 ViewB 的 superView 上的触摸,并从那里移动按钮
  • @Akhilrajtr:这是否意味着我必须检查 Min & Max on touch 移动方法?像这样: - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { float x = self.frame.origin.x;浮动 y = self.frame.origin.y; if (x438) { //取消触摸 } }
  • @Basheer_CAD :我明白你的意思,但是应用程序是这样开发的,所有的触摸都在 UIButton 对象中处理。
  • @Danialzo 你能在- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event中显示代码吗?

标签: ios objective-c ipad uibutton


【解决方案1】:

试试这个,

- (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的子视图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 2013-04-18
    • 1970-01-01
    相关资源
    最近更新 更多