【问题标题】:I would like to move a UIButton to a specific location, like on a grid我想将 UIButton 移动到特定位置,例如在网格上
【发布时间】:2012-05-06 09:15:05
【问题描述】:

大家好, 我有一个带有网格布局的背景图像,并希望允许用户将一组按钮中的一个移动到特定的网格位置,但只能移动到那些特定的位置。换句话说,假设我想将 y 轴上的移动限制为增量,例如 65 像素,然后将按钮对齐到最近的点(如果将其移动 67 像素,它将回退 2 像素)。有谁知道如何做到这一点?

【问题讨论】:

  • 问题是 1) 如何让用户拖动元素或 2) 一旦知道目标坐标后如何移动按钮或 3) 如何将坐标四舍五入到网格上最接近的数字或 4) 全部以上三个?
  • Aloha ckhan,在回答您的问题时,我可以拖动按钮,但想在特定按钮上施加某些动作。换句话说,按钮必须以特定模式移动。我可以找出坐标(通过指定 x 和 y 距离),但需要将按钮限制为仅移动到特定模式中的特定坐标。用户应该能够将按钮移动到该特定坐标,但是让按钮弹回它,按钮被移动到不正确的坐标。

标签: iphone objective-c xcode ipad uibutton


【解决方案1】:

下面是我目前正在使用的一些代码,它允许用户在屏幕上拖放图像。您会注意到“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];
    }
}

【讨论】:

  • 阿罗哈 MDT,感谢您的回复。我会好好看看它,并弄清楚如何根据我的目的对其进行修改。
猜你喜欢
  • 2020-07-09
  • 2016-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 2011-06-12
相关资源
最近更新 更多