2012-01-09 20 views
5

Oto mój kod, aby przenieść UIview 30px następnie do y = 10, ale ta animacja nie działa. To jest moja pierwsza próba stworzenia CAKeyframeAnimation, więc każdy może mi pomóc napisać to poprawnie. Chcę też, aby mój obiekt nie powrócił do pierwotnego miejsca, ale pozostał tam, gdzie skończyła się animacja.CAKeyframeanimation move UIView wzdłuż ścieżki

CGMutablePathRef thePath = CGPathCreateMutable(); 
    CGPathAddRect(thePath, NULL, CGRectMake(self.logo.frame.origin.x, self.logo.frame.origin.y, self.logo.frame.size.width, self.logo.frame.size.height)); 
    CGPathAddRect(thePath, NULL, CGRectMake(self.logo.frame.origin.x, self.logo.frame.origin.y-30, self.logo.frame.size.width, self.logo.frame.size.height)); 
    CGPathAddRect(thePath, NULL, CGRectMake(self.logo.frame.origin.x, 100, self.logo.frame.size.width, self.logo.frame.size.height)); 


    CAKeyframeAnimation* AniLoc = [CAKeyframeAnimation animationWithKeyPath:@"frame"]; 
    AniLoc.path = thePath; 
    AniLoc.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
    AniLoc.keyTimes= [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], 
         [NSNumber numberWithFloat:0.3f], 
         [NSNumber numberWithFloat:1.0f],nil]; 
    AniLoc.duration = 2.0; 

    CFRelease(thePath); 

    [self.logo.layer addAnimation:AniLoc forKey:nil]; 
+0

W jaki sposób "nie działa"? – jrturton

+0

animacja się nie porusza –

Odpowiedz

1

Nie jestem do końca pewien, dlaczego twoja metoda nie działa, ale mogę zaproponować inne rozwiązanie. Zamiast jazdy ramkę, można zmienić położenie przycisku, aby przenieść UIView:

CGMutablePathRef thePath = CGPathCreateMutable(); 
    CGPathMoveToPoint(thePath, NULL, self.logo.frame.origin.x, self.logo.frame.origin.y); // initial point, notice the function 
    CGPathAddLineToPoint(thePath, NULL, self.logo.frame.origin.x - 30, self.logo.frame.origin.y); 
    CGPathAddLineToPoint(thePath, NULL, 100, self.logo.frame.origin.y); 

    CAKeyframeAnimation* AniLoc = [CAKeyframeAnimation animationWithKeyPath:@"position"]; // notice key change 

    // rest is the same 
    AniLoc.path = thePath; 
    AniLoc.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
    AniLoc.keyTimes= [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], 
         [NSNumber numberWithFloat:0.3f], 
         [NSNumber numberWithFloat:1.0f],nil]; 
    AniLoc.duration = 2.0; 

    CFRelease(thePath); 

    [self.logo.layer addAnimation:AniLoc forKey:nil]; 

Mam nadzieję, że pomaga.

+1

Nie działa, ponieważ ramka jest właściwością pochodną. Jeśli chcesz animować klatkę, animuj granice i pozycję. – alecail

Powiązane problemy