我有一个带有网格布局的背景图像,并希望允许用户将一组按钮中的一个移动到特定的网格位置,但仅限于那些特定的位置.换句话说,我想将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,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移动到特定位置,就像在网格上一样所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)