如何使用UIPanGestureRecognizer移动对象? iPhone/iPad

有几个UIPanGestureRecognizer类的例子。例如我读了 this,我仍然无法使用它…

在我正在工作的nib文件我有一个UIView(图像上的白色矩形),我想用该类拖动:

并在我的.m文件中我放置:

- (void)setTranslation:(CGPoint)translation inView:(UIView *)view
{
    NSLog(@"Test to see if this method gets executed");
}

并且当我将鼠标拖动到UIView时,该方法不会被执行。我也试过放置:

- (void)pan:(UIPanGestureRecognizer *)gesture
{
    NSLog(@"testing");
}

而且该方法也不执行。也许我错了,但我认为这个方法应该工作像 – (void)touchesMoved:(NSSet *)触摸withEvent:(UIEvent *)事件方法,我只需要放置该方法,它会被调用每当有触摸。

我究竟做错了什么?也许我必须绘制一个连接到该方法?如果是这样,我怎么能这样做?

解决方法

我发现教程 Working with UIGestureRecognizers,我认为这是我正在寻找。它帮助我想出了以下解决方案:

-(IBAction) someMethod {
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOftouches:1];
    [panRecognizer setMaximumNumberOftouches:1];
    [ViewMain addGestureRecognizer:panRecognizer];
    [panRecognizer release];
}

-(void)move:(id)sender {
    [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);

    [[sender view] setCenter:translatedPoint];

    if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
        CGFloat veLocityX = (0.2*[(UIPanGestureRecognizer*)sender veLocityInView:self.view].x);


        CGFloat finalX = translatedPoint.x + veLocityX;
        CGFloat finalY = firstY;// translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender veLocityInView:self.view].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;
            }
        }

        CGFloat animationDuration = (ABS(veLocityX)*.0002)+.2;

        NSLog(@"the duration is: %f",animationDuration);

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:animationDuration];
        [UIView setAnimationCurve:UIViewAnimationCurveEaSEOut];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(animationDidFinish)];
        [[sender view] setCenter:CGPointMake(finalX,finalY)];
        [UIView commitAnimations];
    }
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...