iphone - 我想将 UIButton 移动到特定位置,例如在网格上

标签 iphone objective-c xcode ipad 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];
    }
}

关于iphone - 我想将 UIButton 移动到特定位置,例如在网格上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10469427/

相关文章:

iphone - 对象的 iOS 5 'Facial Recognition'

iphone - 将 subview 添加到更漂亮的 View 中?

ios - 从子类 setDelegate 设置父类(super class)委托(delegate)

objective-c - 具有贝塞尔曲线和恒定速度的 CAKeyframeAnimation

xcode - 尝试将新版本上传到 iTunes Connect 时出现无效的 Bundle 错误

iphone - 如何拉伸(stretch)图像以填充 UILabel 背景中设置的标签宽度?

iphone - UITabBarItem appearance -setTitleTextAttributes 不断记录 "state = 1 is interpreted as UIControlStateHighlighted"?

ios - 如何检测 NSString 是否包含 NSTextAttachment 图像?

swift - 如何快速修复 "Cannot invoke initializer for type ' MBMovingGround ?' with no arguments"错误

ios - 向下滚动时(或从 ipad 更改为 iphone 时)TableViewCell 边框和阴影