【问题标题】:Restricting button movement (pan & rotation) out of screen bounds限制按钮移动(平移和旋转)超出屏幕范围
【发布时间】:2018-10-18 07:59:42
【问题描述】:

我正在构建键盘应用程序,屏幕上有很多按钮,所有视图(每个视图都包含按钮)都可以旋转或拖动。到目前为止,每个视图都可以被拖动和旋转,就像视图上的按钮可以轻松移动到屏幕边界一样。如何限制视图旋转和拖动,以便所有按钮始终在屏幕上可见并且不能被拖动/旋转出界?

    // Handling dragging to allow user to place views in the whatever places they want

    for (UIView *allView in self.viewArray) {

        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
                                                        initWithTarget:self
                                                        action:@selector(handlePan:)];

        [allView addGestureRecognizer:panGestureRecognizer];

    }

    // Rotate keyboard view
    for (UIView *allViews in self.viewArray) {
        UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationSwitchChanged:)];
        [allViews addGestureRecognizer:rotate];
        NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"appName"];
        CGFloat rotation = [defaults floatForKey:@"rotationAngle"];
        allViews.transform = CGAffineTransformMakeRotation(rotation);
    }
}

- (void) handlePan:(UIPanGestureRecognizer*) sender {

    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"appName"];
    NSString *_value= [defaults stringForKey:@"stateOfSwitchKeyboardDragging"];
    if([_value compare:@"ON"] == NSOrderedSame){

    CGPoint translatedPoint = [sender translationInView:self.view];

    if(sender.state == UIGestureRecognizerStateBegan)
    {
        firstX = [[sender view] center].x;
        firstY = [[sender view] center].y;
    }
    translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

    if ( ( (translatedPoint.x>0) && (translatedPoint.x<self.view.bounds.size.width) ) && ( (translatedPoint.y>0) && (translatedPoint.y<self.view.bounds.size.height) ) )
        [[sender view] setCenter:translatedPoint];
    }
}

- (CGFloat)degreeToRadians:(CGFloat) degree {
    CGFloat radian = degree * (M_PI/180);
    return radian;
}


- (CGFloat)radianToDegree:(CGFloat) radian {
    CGFloat degree = radian * (180/M_PI);
    return degree;
}


- (void) rotationSwitchChanged: (UIRotationGestureRecognizer*) gesture {

    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"appName"];
    NSString *_value= [defaults stringForKey:@"stateOfSwitchKeyboardRotation"];
    if([_value compare:@"ON"] == NSOrderedSame){

        CGFloat radians = atan2f(gesture.view.transform.b, gesture.view.transform.a);

        CGFloat newRadian = gesture.rotation;

        CGFloat degrees = [self radianToDegree:(radians + newRadian)];

        if ( (degrees<=30) && (degrees>=(-30)) ) {
            for (UIView *rotationView in self.viewArray)
                rotationView.transform = CGAffineTransformMakeRotation(newRadian);
            self.rotationAngle = newRadian;
            [defaults setFloat:self.rotationAngle forKey:@"rotationAngle"];
        }
    }
}

【问题讨论】:

  • 在您的平移手势处理程序中,您检查最终点是否在屏幕边界内,然后设置视图的中心(如果是)。但这可能会导致视图超出父视图的范围。您需要检查端点+视图的各自尺寸是否超出父视图的边界。
  • 你能发一个示例代码吗?

标签: ios objective-c rotation custom-keyboard pan


【解决方案1】:

您始终可以使用以下代码获取手势识别器的位置。针对 Objective-C 进行相应修改。

let location: CGPoint = gesturerecognizer.location(in: view)

写一个 if 条件来检查这个位置是否在边界内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-06
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多