我正在使用UIPanGesturerecogniser
在屏幕上移动UIButton
。
我正以这种方式将手势添加到按钮:
panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[ButtonNote addGestureRecognizer:panRecognizer];
,方法是
- (void)move:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.ViewA];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
[self.view addSubview:[(UIPanGestureRecognizer*)sender view]];
if (self.Button1.frame.size.height > 200) {
}
else {
}
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded||[(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateCancelled||[(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateFailed){
CGPoint fingerPoint2 = [(UIPanGestureRecognizer*)sender locationInView:self.BallBin.superview];
if (CGRectContainsPoint(self.BallBin.frame, fingerPoint2)) {
if (self.Button3.frame.size.height > 200) {
UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)sender;
UIButton *button = (UIButton *)gesture.view;
[[(UIPanGestureRecognizer*)sender view] removeFromSuperview];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *myFilePath = [documentsDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", button.titleLabel.text]];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:myFilePath error:NULL];
[self performSelector:@selector(DeleteBusinessCard:) withObject:nil afterDelay:0.5];
}
else {
[self.ViewA addSubview:[(UIPanGestureRecognizer*)sender view]];
}
}
if (self.Button1.frame.size.height > 200) {
}
else {
[self.ViewB addSubview:self.BallBin];
}
}];
}
CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.ViewA].x);
CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.ViewA].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 768) {
finalX = 768;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 1024) {
finalY = 1024;
}
}
else {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 1024) {
finalX = 768;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 768) {
finalY = 1024;
}
}
}
}
我想限制按钮在屏幕上某个圆圈区域的移动,例如半径80.现在按钮正在整个屏幕上移动。无论如何,ViewA是覆盖整个屏幕的UIView
。
答案 0 :(得分:1)
使用Pitagoras公式计算当前位置和起点之间的距离。在你的情况下:
if (gesture.state == UIGestureRecognizerStateChanged)
{
if (sqrt(deltaX*deltaX + deltaY*deltaY) < 80)
move view to finger's position
else
stay where you are
}
答案 1 :(得分:0)
我们可以有一个逻辑来计算与特定中心的协调。
当Pan Gesture发生时,该函数应该修改平移对象的中心, 以下代码中的示例,名为pin的pannig对象,这是一个50x50点的图像, 将被设置为绕过一个194x194点的大圆形图像,位于63,141。
这是PanGesture处理程序代码:
- (void)pinPanPiece:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint center = CGPointMake(63.0 + 194.0/2, 141.0+ 194.0/2);
UIView *piece = [gestureRecognizer view];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
if (piece == pin) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
float newX = piece.frame.origin.x + translation.x;
CGRect newFrame = piece.frame;
CGPoint pinCenter = CGPointMake(newFrame.origin.x + newFrame.size.width / 2.0, newFrame.origin.y + newFrame.size.height / 2.0);
newFrame.origin.x = newX;
CGPoint newPinCenter = CGPointMake(newFrame.origin.x + newFrame.size.width / 2.0, newFrame.origin.y + newFrame.size.height / 2.0);
float detectY = pinCenter.y + translation.y;
BOOL bUpCenter = detectY < center.y;
float deltaX = center.x - newPinCenter.x;
float deltaY = sqrtf((97.0 *97.0) - (deltaX * deltaX));
float newY;
if (bUpCenter) {
newY = center.y - deltaY;
}
else {
newY = center.y + deltaY;
}
newY -= newFrame.size.height / 2.0;
newFrame.origin.y = newY;
if ((newPinCenter.x >= 63.0) && (newPinCenter.x <= 63.0+194.0)) {
[piece setFrame:newFrame];
}
}
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}