UIImageView animationImages Cannot Be Changed?
I am animating images with UIImageView:
imageView.animationImages = [NSArray arrayWithArray:imgNameArr];
imageView.animationDuration = 2;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
At some point later I am trying to change images to some new ones:
[imageView stopAnimating];
imageView.animationImages = nil;
[imageView.animationImages release];
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
nil];
[imageView setAnimationDuration:1];
[imageView startAnimating];
This does NOT work. My app either crashes or images disappear. I've seen a few people have the same problem and they create various workaround. Is something wrong with my code? Can something be done?
In addition I discovered that with this code below the object disappears at end of animation.
int x = rect.origin.x;
int y = rect.origin.y;
int w = rect.size.width;
float _frequency 开发者_StackOverflow= 0;
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = speed;
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
CGMutablePathRef pointPath = CGPathCreateMutable();
CGPathMoveToPoint(pointPath, NULL, rect.origin.x, rect.origin.y);
do {
CGFloat yVal = sin (_frequency * M_PI/180);
CGPathAddLineToPoint(pointPath, NULL, x, y + yVal * 15);
_frequency += freq;
x--;
} while (x > w);//does not go outside
pathAnimation.path = pointPath;
CGPathRelease(pointPath);
[imageView.layer addAnimation:pathAnimation forKey:@"pathAnimation"];
[imageView release];
Since animationImages is a property, you don't need to set it to nil, and releasing it AFTER you set it to nil does nothing (it sends release
to nil). Releasing it beforehand will cause a crash. Just set it to the new value, or to nil. You shouldn't be releasing another object's properties.
I do not know why your code does not work.
It is my sample. It works fine.
animationView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
NSMutableArray *animationImages = [[NSMutableArray alloc] init];
for (int i=0; i < [contents count]; i++) {
[animationImages addObject: [UIImage imageNamed:[contents objectAtIndex:i]]];
}
animationView.animationImages = animationImages;
animationView.animationDuration = playTime;
animationView.animationRepeatCount = 0;
[self.view addSubview:animationView];
[animationView startAnimating];
精彩评论