2012-07-12 22 views
5

Tôi muốn có thể sử dụng một UIViewAnimationCurve cho phép quay và một thay đổi vị trí khác. Điều này có thể không?Sử dụng hai UIViewAnimationCurves khác nhau trong cùng một hoạt ảnh

Ví dụ: (trong mã giả);

// Begin animations 

// Set rotation animation curve to EaseInOut 

// Set position animation curve to Linear 

// Make some changes to position 

// Make some change to the angle of rotation 

// Commit the animations 

EDIT: (cách tiếp cận CAAnimationGroup gợi ý dưới đây) - Có tạo ra 2 CABasicAnimations riêng biệt và một CAAnimationGroup tuy nhiên các hình ảnh động không được bắt đầu. Bất kỳ ý tưởng?

CABasicAnimation *postionAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
postionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
postionAnimation.toValue = [NSValue valueWithCGPoint:[self transformPointToWorldSpaceFromViewSpace:self.spriteView.position]]; 
postionAnimation.delegate = self; 

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.z"]; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
rotationAnimation.toValue = [NSNumber numberWithFloat:self.spriteView.angle -= M_PI_2]; 
rotationAnimation.delegate = self; 

CAAnimationGroup *animationsGroup = [CAAnimationGroup animation]; 
animationsGroup.duration = self.clockSpeed; 
animationsGroup.animations = [NSArray arrayWithObjects:postionAnimation, rotationAnimation, nil]; 

// Perform the animation 
[self.spriteView.layer addAnimation:animationsGroup forKey:nil]; 

Trả lời

3

Tốt nhất là sử dụng nhiều cuộc gọi đến animateWithDuration: chậm trễ: lựa chọn: hình ảnh động: hoàn :. Sử dụng đường cong hoạt hình khác nhau trong mỗi cuộc gọi và chúng sẽ chạy song song. Hoặc, với các giá trị độ trễ khác nhau, bạn có thể làm cho chúng chạy theo thứ tự.

Mã này có thể trông như thế này:

[UIView animateWithDuration: .5 
    delay: 0 
    options: UIViewAnimationOptionCurveEaseInOut 
    animations: ^{ 
    view1.center = CGPointMake(x, y); 
    } 
    completion: ^{ 
    //code that runs when this animation finishes 
    } 
]; 

[UIView animateWithDuration: .5 
    delay: .5 
    options: UIViewAnimationOptionCurveLinear 
    animations: ^{ 
    view2.center = CGPointMake(x2, y2); 
    } 
    completion: ^{ 
    //code that runs when this animation finishes 
    } 
]; 

Nếu bạn sử dụng CAAnimationGroups, có một số vấn đề.

Đầu tiên, nhóm hoạt ảnh xác định đường cong hoạt ảnh cho toàn bộ nhóm. Tôi không nghĩ rằng bạn có thể chỉ định các đường cong khác nhau cho từng hoạt ảnh phụ (mặc dù tôi thú nhận mình chưa thử nó).

Thứ hai, nếu bạn thêm hoạt ảnh vào nhóm, các đại biểu đến từng hoạt ảnh không nhận được gọi là. Chỉ các phương thức ủy nhiệm của nhóm hoạt ảnh mới được gọi.

+0

Đây là lời khuyên tuyệt vời, cảm ơn Duncan. Sẽ cho nó đi vào ngày mai. Dave –

+0

Xin chào Duncan, đã làm điều này và làm việc một điều trị! Câu hỏi duy nhất tôi có là, nó là ok để gọi phương pháp của tôi [tự transformPointToViewSpaceFromWorldSpace: self.spriteView.position] trong khối? Nó hoạt động và các công cụ không hiển thị bất kỳ rò rỉ, tuy nhiên tôi mơ hồ nhớ một cái gì đó về việc sử dụng tự trong khối. Cảm ơn một lần nữa, Dave. –

4

Hãy thử tạo hai hoạt ảnh khác nhau. Với phương pháp UIView, bạn không thể đặt các hoạt ảnh khác nhauCấu hình cho các thuộc tính khác nhau của hoạt ảnh của bạn. Hoặc bạn có thể vui chơi với CAAnimations và tạo một nhóm CAAnimationGroup trong đó bạn đặt hai CABasicAnimation

+0

Cảm ơn iSofTom, đã thử phương pháp này (xem chỉnh sửa được đề cập) nhưng theo phương pháp đại biểu hoạt ảnh của tôi không bắt đầu. Bất kỳ ý tưởng? –

0

Ok, cuối cùng đã giải quyết được. Cảm ơn bạn đã chỉ đạo iSofTom (đã bỏ phiếu cho câu trả lời của bạn). Tôi đã đặt các đại biểu của các hoạt ảnh riêng lẻ chứ không phải nhóm.

CABasicAnimation *postionAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
postionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
postionAnimation.fromValue = [NSValue valueWithCGPoint:self.spriteView.center]; 
postionAnimation.toValue = [NSValue valueWithCGPoint:[self transformPointToViewSpaceFromWorldSpace:self.spriteView.position]]; 

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
rotationAnimation.fromValue = [NSNumber numberWithFloat:self.spriteView.angle]; 
rotationAnimation.toValue = [NSNumber numberWithFloat:self.spriteView.rotation]; 

CAAnimationGroup *animationsGroup = [CAAnimationGroup animation]; 
animationsGroup.duration = self.clockSpeed; 
animationsGroup.animations = [NSArray arrayWithObjects:postionAnimation, rotationAnimation, nil]; 
animationsGroup.delegate = self; 
// Perform the animation 
[self.spriteView.layer addAnimation:animationsGroup forKey:nil]; 

- (void)animationDidStart:(CAAnimation *)theAnimation { 
radiansToDegrees(self.spriteView.rotation)); 
    self.spriteView.angle = self.spriteView.rotation; 

NSStringFromCGPoint(self.spriteView.position)); 
    self.spriteView.center = [self transformPointToViewSpaceFromWorldSpace:self.spriteView.position]; 
} 
Các vấn đề liên quan