3
我想動畫的圖像序列,這可以很容易地完成與簡單的UIImageView動畫;不過,我希望能夠檢測動畫何時完成,以便我可以允許用戶執行另一項任務。所以我試圖用CAKeyframeAnimation來做到這一點。當我運行以下代碼時,程序崩潰了。有人能告訴我我做錯了什麼嗎?使用CAKeyframeAnimation動畫序列的圖像
CALayer *myLayer = [[CALayer alloc]init];
[myLayer setBounds:CGRectMake(0, 0, 100, 100)];
[myLayer setPosition:CGPointMake(160.0, 100.0)];
myLayer.backgroundColor = [[UIColor colorWithRed:4 green:0 blue:0 alpha:1] CGColor];
[self.window.layer addSublayer:myLayer];
CAKeyframeAnimation *anim;
NSArray *images =[NSArray arrayWithObjects:[UIImage imageNamed:@"img1.png"],
[UIImage imageNamed:@"img2.png"],nil];
anim = [CAKeyframeAnimation animation];
[anim setKeyPath:@"contents"];
[anim setValues:images];
[anim setCalculationMode:@"discrete"];
[anim setRepeatCount:3];
[anim setDuration:1.0];
[myLayer addAnimation:anim forKey:nil];
這可能會崩潰,因爲您將值設置爲'UIImage'而不是'CGImageRef'這是'contents'屬性所需的值。 –