iOS   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 我想将UIButton移动到特定位置,就像在网格上一样大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

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

解决方法

下面是我正在使用的一些代码,它允许用户在屏幕上拖放图像.你会注意到“if”语句的值设置为“960”和“640”等等,这表明如果用户试图将图像拖离屏幕,它会动画图像移回屏幕并且可以很容易地修改使图像移动到用户放在附近的最近的网格坐标.

- (void)callMarkerFourteen
{

    UIImage *image = [UIImage imag@R_607_8371@d:@"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,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移动到特定位置,就像在网格上一样全部内容,希望文章能够帮你解决iphone – 我想将UIButton移动到特定位置,就像在网格上一样所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。