在DetailViewController中使用Touches移动UIView

时间:2014-08-21 23:06:04

标签: xcode uiview floating touches

我正在尝试在使用SplitViewController创建的应用中创建浮动键盘。我正在使用Touches选择键盘视图,移动它,并将其边界限制在DetailViewController的边缘减去一些指定的边界。

我可以在单视图应用程序中轻松创建浮动键盘,但在SplitViewController应用程序中,每次我尝试移动键盘时,它只移动几个像素然后停止。

因此,我可以得出结论,SplitViewController有一些导致问题的原因。有修复或解决方法吗?

以下是代码:

BOOL dragging;
float startX = 0;
float startY = 0;
float marginLeft = 20;
float marginRight = 20;
float marginTop = 150;
float marginBottom = 75;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    dragging = NO;
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:touch.view];

    if ([[touch.view class] isSubclassOfClass:[UIView class]]) {
        if ([touch view] == keypadFloatView) {
            startX = touchLocation.x;
            startY = touchLocation.y;
            dragging = YES;
        }
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    dragging = NO;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:touch.view];

    if ([[touch.view class] isSubclassOfClass:[UIView class]]) {
        if ([touch view] == keypadFloatView) {
            if (dragging) {
                float moveViewW = keypadFloatView.frame.size.width;
                float moveViewH = keypadFloatView.frame.size.height;

                CGRect frame = keypadFloatView.frame;
                frame.origin.x = keypadFloatView.frame.origin.x + touchLocation.x - startX;
                frame.origin.y = keypadFloatView.frame.origin.y + touchLocation.y - startY;

                if (frame.origin.x < marginLeft) { frame.origin.x = marginLeft; }
                if (frame.origin.x > DEVICE_SIZE.width - marginRight - moveViewW) { frame.origin.x = DEVICE_SIZE.width - marginRight - moveViewW; }
                if (frame.origin.y < marginTop) { frame.origin.y = marginTop; }
                if (frame.origin.y > DEVICE_SIZE.height - marginBottom - moveViewH) { frame.origin.y = DEVICE_SIZE.height - marginBottom - moveViewH; }

                keypadFloatView.frame = frame;
             }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尤里卡。这段代码没问题。问题在于iPad DetailViewController屏幕尺寸是“固定的”。我将大小更改为“自由形式”并将其定义为768 x 1024并且问题消失了。实际上,还有一个问题,但是性质不同,即触摸和随后的移动有时会导致MasterViewController从左侧滑入。为了解决这个问题,我在AppDelegate中禁用了“gesture”默认设置。