简体   繁体   中英

Restricting button movement (pan & rotation) out of screen bounds

I am building Keyboard app with a lot of buttons on the screen and all views (each view contains buttons) can be rotated or dragged. As of now each view can be dragged and rotated in the way that buttons on the views can be easily moved put of screen bounds. How can I limit views rotation and dragging so that all buttons are always visible on screen and can't be dragged/rotated out of bounds?

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

You can always get the location of gesture recognizer using below code. Modify accordingly for Objective-C.

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

Write an if condition to check if this location falls inside the bounds or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM