iphone – 我想将UIButton移动到特定位置,就像在网格上一样

阿罗哈大家,

  我有一个带有网格布局的背景图像,并希望允许用户将一组按钮中的一个移动到特定的网格位置,但仅限于那些特定的位置.换句话说,我想将y轴上的移动限制为65像素的增量,然后将按钮捕捉到最近的点(如果你移动了67个像素,它会快照回来2个像素).有谁知道如何做到这一点? 最佳答案 下面是我正在使用的一些代码,它允许用户在屏幕上拖放图像.你会注意到“if”语句的值设置为“960”和“640”等等,这表明如果用户试图将图像拖离屏幕,它会动画图像移回屏幕并且可以很容易地修改使图像移动到用户放在附近的最近的网格坐标.

- (void)callMarkerFourteen
{

    UIImage *image = [UIImage imageNamed:@"VerticalLine.png"];

    markerViewFourteen = [[UIView alloc] initWithFrame:CGRectMake(160, 210, 40, image.size.height)];
    [markerViewFourteen setBackgroundColor:[UIColor colorWithRed:255 green:0 blue:0 alpha:.5]];
    markerImageViewFourteen = [[UIImageView alloc] initWithFrame:[markerViewFourteen frame]];
    [markerImageViewFourteen setFrame:CGRectMake(18, 0, 4, 100)];
    [markerImageViewFourteen setImage:image];
    [markerViewFourteen addSubview:markerImageViewFourteen];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [markerViewFourteen addGestureRecognizer:panRecognizer];

    [self.view addSubview:markerViewFourteen];
}


-(void)move:(id)sender {

    [[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];

    [self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

        firstX = [[sender view] center].x;
        firstY = [[sender view] center].y;
    }

    translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

    [[sender view] setCenter:translatedPoint];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

        CGFloat finalX = translatedPoint.x + (0.0*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
        CGFloat finalY = translatedPoint.y + (0.0*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);

        if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {

            if(finalX < 0) {                                
                finalX = 0;             
            }                           
            else if(finalX > 640) {

                finalX = 640;
            }

            if(finalY < 0) {                                
                finalY = 0;             
            }                       
            else if(finalY > 960) {

                finalY = 960;
            }
        }

        else {

            if(finalX < 0) {                                
                finalX = 0;             
            }                       
            else if(finalX > 960) {

                finalX = 640;
            }

            if(finalY < 0) {                                
                finalY = 0;             
            }                       
            else if(finalY > 640) {

                finalY = 960;
            }
        }

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.35];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [[sender view] setCenter:CGPointMake(finalX, finalY)];
        [UIView commitAnimations];
    }
}
点赞