2012-01-09 31 views
5

Đây là mã của tôi để di chuyển UIview 30px xuống rồi tối đa y = 10 nhưng hoạt ảnh này không hoạt động. Đây là nỗ lực đầu tiên của tôi để tạo CAKeyframeAnimation để bất kỳ ai cũng có thể giúp tôi viết chính xác. Ngoài ra tôi muốn đối tượng của tôi không quay trở lại vị trí ban đầu nhưng vẫn ở đó, nơi hoạt ảnh kết thúc.CAKeyframeanimation di chuyển UIView dọc theo đường dẫn

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

Bằng cách nào "không hoạt động"? – jrturton

+0

hoạt ảnh không di chuyển bất cứ điều gì –

Trả lời

1

Tôi không chắc chắn lý do tại sao phương pháp của bạn không hoạt động, nhưng tôi có thể đề xuất giải pháp khác. Thay vì lái xe khung, bạn có thể thay đổi khóa vị trí để di chuyển 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]; 

Tôi hy vọng điều này sẽ giúp bạn.

+1

Nó không hoạt động vì khung là một thuộc tính có nguồn gốc. Nếu bạn cần tạo hiệu ứng cho khung hình, bạn tạo hiệu ứng cho các giới hạn và vị trí. – alecail

Các vấn đề liên quan