2012-04-14 32 views
5

Sử dụng Obj.-c cho iPhone 5.1 trong Xcode 4.3.2; Tôi tạo ra một mảng của CALayers, tất cả từ cùng một hình ảnh. Sau đó, tôi muốn áp dụng CABasicAnimation cho mỗi CALayer trong mảng cùng một lúc bằng cách nhóm thông qua Giao dịch CAT. Điều này tất cả các công trình một thời gian. Tuy nhiên, tôi muốn liên tục gọi khối CABasicAnimations được chứa trong CATransactions nhưng có thể sửa đổi các thuộc tính của từng hoạt ảnh riêng lẻ mỗi khi khối được thực hiện đồng thời. Ví dụ, tôi có từ và giá trị cho hoạt ảnh mà tôi muốn thay đổi ngẫu nhiên mỗi lần cho hoạt ảnh trên mỗi lớp. Bởi vì tôi muốn lặp lại cùng một hình động cơ bản, nhưng thay đổi thuộc tính; Tôi đã thử gọi phương thức animate liên tục bằng cách sử dụng vòng lặp for trong phương thức makeSwarm, bằng cách sử dụng animationDidStop để tạo ra một cuộc gọi khác của phương thức animate nhưng những gì kết thúc xảy ra là một cuộc gọi mới là được thực hiện với khối CATransaction và không phải ở cuối, và cũng có phương thức gọi chính nó (đặt [animate]; ở cuối phương thức animate); và không cái nào trong số này hoạt động. Đây là mã cơ bản. Tôi cho rằng điều này là đơn giản, nhưng tôi không thấy cái gì quan trọng. Cảm ơn, Sethlàm thế nào để lặp lại một số CABasicAnimations chứa trong một khối Giao dịch?

các ViewController.h

#import <QuartzCore/QuartzCore.h> 
#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController{ 
    UIImage *beeImage; 

    UIImageView *beeView; 
    CALayer *beeLayer; 
    CABasicAnimation *animat; 
    NSMutableArray *beeArray; 
    NSMutableArray *beeanimArray; 

} 

@property(retain,nonatomic) UIImage *beeImage; 
@property(retain,nonatomic) NSMutableArray *beeArray; 
@property(retain,nonatomic) NSMutableArray *beeanimArray; 
@property(retain,nonatomic) UIImageView *beeView; 
@property(retain,nonatomic) CALayer *beeLayer; 
@property(retain,nonatomic)CABasicAnimation *animat; 
-(void) animate; 
-(void) makeSwarm; 


@end 

các ViewController.m

-(void) makeSwarm{ 

    self.view.layer.backgroundColor = [UIColor orangeColor].CGColor; 
    self.view.layer.cornerRadius = 20.0; 
    self.view.layer.frame = CGRectInset(self.view.layer.frame, 20, 20); 

    CGRect beeFrame; 
    beeArray = [[NSMutableArray alloc] init]; 
    beeImage = [UIImage imageNamed:@"bee50x55px.png"]; 
    beeFrame = CGRectMake(0, 0, beeImage.size.width, beeImage.size.height); 


    int i; 

    CALayer *p = [[CALayer alloc] init]; 


    for (i = 0; i < 3; i++) { 



     beeView = [[UIImageView alloc] initWithFrame:beeFrame]; 
     beeView.image = beeImage;  
     beeLayer = [beeView layer]; 
     [beeArray addObject: beeLayer]; 


     p = [beeArray objectAtIndex: i];  

     [p setPosition:CGPointMake(arc4random()%320, arc4random()%480)]; 
     [self.view.layer addSublayer:p]; 



    } 



    [self animate]; 

} 

-(void)animate 
{ 
    //the code from here to the end of this method is what I would like to repeat as many times as I would like 
    [CATransaction begin]; 

    int i; 
    for (i = 0; i < 3; i++) { 

     animat = [[CABasicAnimation alloc] init]; 
     [animat setFromValue:[NSValue valueWithCGPoint:CGPointMake(arc4random()%320, arc4random()%480)]]; 
     animat.toValue = [NSValue valueWithCGPoint:CGPointMake(arc4random()%320, arc4random()%480)]; 
     [animat setFillMode:kCAFillModeForwards]; 
     [animat setRemovedOnCompletion:NO]; 
     animat.duration=1.0; 


     CALayer *p = [[CALayer alloc] init]; 
     p = [beeArray objectAtIndex: i]; 
     [p addAnimation:animat forKey:@"position"]; 



    }  

    [CATransaction commit];  


} 
+0

Tôi tin rằng tôi đã tự mình trả lời câu hỏi này. Tôi đặt đại biểu cho hoạt ảnh ở cuối vòng lặp (khi i == 2) và khi hoạt ảnh đó kết thúc (chỉ ra vòng lặp kết thúc), sau đó từ phương thức animationDidStop, tôi gọi phương thức này sẽ hoạt động trở lại. Nếu có một giải pháp miễn phí thanh lịch hoặc rắc rối hơn này, tôi là tất cả tai và sẽ chấp nhận nó như là câu trả lời. –

Trả lời

2

Tôi tin rằng tôi đã trả lời này cho bản thân mình. Tôi đặt đại biểu cho hoạt ảnh ở cuối vòng lặp (khi i == 2) và khi hoạt ảnh đó kết thúc (chỉ ra vòng lặp kết thúc), sau đó từ phương thức animationDidStop, tôi gọi phương thức này sẽ hoạt động trở lại. Nếu có một giải pháp miễn phí thanh lịch hoặc rắc rối hơn này, tôi là tất cả tai và sẽ chấp nhận nó như là câu trả lời.

+0

Trong trường hợp bất kỳ ai cần mã cho những gì tôi đã nêu ở trên, hãy chèn mã này: animat.duration = 2; nếu (i == 0) animat.delegate = tự; vào phương thức animate và sau đó thêm phương thức này làm phương thức khác - (void) animationDidStop: (CAAnimation *) theAnimation đã hoàn thành: (BOOL) flag {[self animate];} –

+0

Stack cuối cùng sẽ được lấp đầy với các cuộc gọi phương thức animation? –

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