限制按钮移动(平移和旋转)超出屏幕范围

时间:2018-05-08 11:21:23

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

我正在使用屏幕上的许多按钮构建键盘应用程序,并且可以旋转或拖动所有视图(每个视图包含按钮)。截至目前,每个视图都可以按照视图上的按钮轻松移动屏幕边界的方式进行拖动和旋转。如何限制视图旋转和拖动,以便所有按钮始终在屏幕上可见,并且无法拖动/旋转超出范围?

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

1 个答案:

答案 0 :(得分:0)

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

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

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