SORU
13 Temmuz 2011, ÇARŞAMBA


Nasıl UİPanGestureRecognizer nesnesini taşımak için? iPhone/iPad

UIPanGestureRecognizer sınıfa çeşitli örnekler vardır. Örneğin this okudum ve hala kullanabiliyorum

Üzerinde çalışıyorum bu uç dosya üzerinde bu sınıf ile sürüklemek istiyorum UIView (görüntü üzerinde beyaz dikdörtgen) var:

enter image description here

ve benim .m yerleştirdim dosya:

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

ve bu yöntem UIView üzerinde fareyi sürükleyin zaman yerine getirilmeyecek. Ayrıca yerleştirerek denedim:

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

Ve bu yöntem de yerine getirilmeyecek. Belki yanılıyorum ama bu yöntem dokunur olduğunda ben sadece bu yöntem yer var ve Adı alacak - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event yöntemi gibi çalışması gerektiğini düşünüyorum.

Neyi yanlış yapıyorum? Bu yöntem için bir bağlantı çizmek için belki de? Bunu nasıl yapabilirim?

CEVAP
14 Temmuz 2011, PERŞEMBE


Working with UIGestureRecognizers ve ben arıyorum ne olduğunu düşünüyorum öğretici buldum. Bana aşağıdaki çözüm ile gelip yardımcı oldu:

-(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];
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BuzzFeedVideo

    BuzzFeedVide

    10 AĞUSTOS 2011
  • LimeFire

    LimeFire

    2 ŞUBAT 2012
  • TopDJMag TV

    TopDJMag TV

    29 Temmuz 2010