2009-07-17 13 views
78

Tôi đang phát triển một ứng dụng thương mại. Khi tôi thêm một mục vào giỏ hàng, tôi muốn tạo hiệu ứng trong đó hình ảnh của mục đó theo một đường cong và kết thúc tại tab giỏ hàng.Làm cách nào để tạo hiệu ứng chuyển động của một chế độ xem hoặc hình ảnh dọc theo một đường cong?

Làm cách nào để tạo hoạt ảnh của hình ảnh dọc theo đường cong như thế này?

+0

[Hình ảnh động trên iPhone] (http://www.cuppadev.co.uk/animated-images-on-the-iphone-sans-memory-leaks/) Tham khảo liên kết này. Nó sẽ giúp bạn chắc chắn. Cảm ơn. – SALMAN

Trả lời

2

Bạn có thể tạo hiệu ứng động cho thuộc tính trung tâm của UIView bằng cách sử dụng CAKeyframeAnimation. Xem hướng dẫn lập trình CoreAnimation.

152

Để mở rộng theo những gì Nikolai đã nói, cách tốt nhất để xử lý việc này là sử dụng Core Animation để tạo hiệu ứng chuyển động của hình ảnh hoặc xem dọc theo một con đường Bezier. Điều này được thực hiện bằng cách sử dụng CAKeyframeAnimation. Ví dụ, tôi đã sử dụng đoạn mã sau để animate một hình ảnh của một cái nhìn vào một biểu tượng để chỉ tiết kiệm (như có thể thấy trong video for this application):

Trước hết tập tin tiêu đề nhập khẩu QuartzCore #import <QuartzCore/QuartzCore.h>

UIImageView *imageViewForAnimation = [[UIImageView alloc] initWithImage:imageToAnimate]; 
imageViewForAnimation.alpha = 1.0f; 
CGRect imageFrame = imageViewForAnimation.frame; 
//Your image frame.origin from where the animation need to get start 
CGPoint viewOrigin = imageViewForAnimation.frame.origin; 
viewOrigin.y = viewOrigin.y + imageFrame.size.height/2.0f; 
viewOrigin.x = viewOrigin.x + imageFrame.size.width/2.0f; 

imageViewForAnimation.frame = imageFrame; 
imageViewForAnimation.layer.position = viewOrigin; 
[self.view addSubview:imageViewForAnimation]; 

// Set up fade out effect 
CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
[fadeOutAnimation setToValue:[NSNumber numberWithFloat:0.3]]; 
fadeOutAnimation.fillMode = kCAFillModeForwards; 
fadeOutAnimation.removedOnCompletion = NO; 

// Set up scaling 
CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"]; 
[resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(40.0f, imageFrame.size.height * (40.0f/imageFrame.size.width))]]; 
resizeAnimation.fillMode = kCAFillModeForwards; 
resizeAnimation.removedOnCompletion = NO; 

// Set up path movement 
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
pathAnimation.calculationMode = kCAAnimationPaced; 
pathAnimation.fillMode = kCAFillModeForwards; 
pathAnimation.removedOnCompletion = NO; 
//Setting Endpoint of the animation 
CGPoint endPoint = CGPointMake(480.0f - 30.0f, 40.0f); 
//to end animation in last tab use 
//CGPoint endPoint = CGPointMake(320-40.0f, 480.0f); 
CGMutablePathRef curvedPath = CGPathCreateMutable(); 
CGPathMoveToPoint(curvedPath, NULL, viewOrigin.x, viewOrigin.y); 
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, viewOrigin.y, endPoint.x, viewOrigin.y, endPoint.x, endPoint.y); 
pathAnimation.path = curvedPath; 
CGPathRelease(curvedPath); 

CAAnimationGroup *group = [CAAnimationGroup animation]; 
group.fillMode = kCAFillModeForwards; 
group.removedOnCompletion = NO; 
[group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, pathAnimation, resizeAnimation, nil]]; 
group.duration = 0.7f; 
group.delegate = self; 
[group setValue:imageViewForAnimation forKey:@"imageViewBeingAnimated"]; 

[imageViewForAnimation.layer addAnimation:group forKey:@"savingAnimation"]; 

[imageViewForAnimation release]; 
+0

Tôi đã nhận được. Nhưng tôi muốn tạo hiệu ứng hoạt hình như một hình ảnh lớn tôi sẽ được đổi kích thước hình ảnh thành trung tâm hình ảnh lớn rồi thêm đường dẫn hoạt hình. Làm thế nào để có được nó. –

+0

Tôi không chắc bạn đang hỏi gì, nhưng hình động trên làm ba việc: di chuyển hình ảnh dọc theo một đường cong, co lại nó khi nó di chuyển sao cho nó nhỏ xíu khi nó chạm vào điểm kết thúc và làm mờ nó khi nó di chuyển. Bạn có thể tinh chỉnh kích thước bằng cách thay đổi giá trị từ hoặc thành giá trị, nếu cần. –

+0

Tôi phải bao gồm những gì để Xcode nhận ra tất cả nội dung của CAKey ...? Nó nói nó không tìm thấy các biểu tượng. – Thanks

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