首页 > 解决方案 > 限制按钮移动(平移和旋转)超出屏幕范围

问题描述

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

    // 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"];
        }
    }
}

标签: iosobjective-crotationcustom-keyboardpan

解决方案


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

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

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


推荐阅读