加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 如何循环CATransaction块中包含的几个CABasicAni

发布时间:2020-12-14 19:13:04 所属栏目:百科 来源:网络整理
导读:在 Xcode 4.3.2中使用Obj.-c for iPhone 5.1;我创建了一个CALayers数组,所有这些都来自同一个图像.然后,我希望通过CATransactions分组,同时将CABasicAnimation应用于阵列中的每个CALayer.这一切都有效.但是,我想重复调用CATransactions中包含的CABasicAnimat
在 Xcode 4.3.2中使用Obj.-c for iPhone 5.1;我创建了一个CALayers数组,所有这些都来自同一个图像.然后,我希望通过CATransactions分组,同时将CABasicAnimation应用于阵列中的每个CALayer.这一切都有效.但是,我想重复调用CATransactions中包含的CABasicAnimations块,但是每次同时执行块时都能够单独修改每个动画的属性.例如,我有动画的值,我想每次为每个图层上的动画随机更改动画.因为我想重复相同的基本动画,但要进行属性修改;将动画的repeatCount属性设置为某个高值将无效.我尝试在makeSwarm方法中使用for循环重复调用animate方法,使用animationDidStop引发另一个animate方法调用但最终发生的是新调用是使用CATransaction块而不是在最后,并且还有方法调用本身(在animate方法的末尾放置[self animate];);这一切都不起作用.这是基本代码.我认为这很简单,但我没有看到重要的事情.谢谢,赛斯

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

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,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];     


}

解决方法

我相信我已经为自己回答了这个问题.我在循环结束时设置了动画的委托(当i == 2时),当动画结束时(指示循环结束),然后从animationDidStop方法我再次调用方法动画.如果有一个比这更优雅或无故障的解决方案,我会全神贯注并接受它作为答案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读