xcode 中的动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6493157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
animation in xcode
提问by user702003
when I doing animation with the next code:
当我用下一个代码做动画时:
NSArray *images=[NSArray arrayWithObjects:[UIImage imageNamed:@"shavit_1.png"],[UIImage imageNamed:@"shavit_1.png"],[UIImage imageNamed:@"shavit_2.png"],[UIImage imageNamed:@"shavit_3.png"],[UIImage imageNamed:@"shavit_4.png"],[UIImage imageNamed:@"shavit_5.png"],[UIImage imageNamed:@"shavit_6.png"],[UIImage imageNamed:@"shavit_7.png"],[UIImage imageNamed:@"shavit_8.png"],[UIImage imageNamed:@"shavit_9.png"],[UIImage imageNamed:@"shavit_10.png"],[UIImage imageNamed:@"shavit_11.png"],[UIImage imageNamed:@"shavit_12.png"],[UIImage imageNamed:@"shavit_13.png"],[UIImage imageNamed:@"shavit_14.png"],[UIImage imageNamed:@"shavit_15.png"],[UIImage imageNamed:@"shavit_16.png"],[UIImage imageNamed:@"shavit_17.png"],[UIImage imageNamed:@"shavit_18.png"],[UIImage imageNamed:@"shavit_19.png"],[UIImage imageNamed:@"shavit_20.png"],[UIImage imageNamed:@"shavit_21.png"],[UIImage imageNamed:@"shavit_22.png"],[UIImage imageNamed:@"shavit_23.png"],[UIImage imageNamed:@"shavit_24.png"],[UIImage imageNamed:@"shavit_25.png"],nil];
shavitimage.animationImages=images;
shavitimage.animationDuration=2.0;
shavitimage.animationRepeatCount=1;
[shavitimage startAnimating];
[images release]; //release the used array
[self performSelector:@selector(animation_ends) withObject:nil afterDelay:2.0];//after 2 seconds put astatic image
timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];
}
-(void)animation_ends{
[shavitimage stopAnimating];//the UIImageView return to static mode
shavitimage.image=[UIImage imageNamed:@"shavit_25.png"];
self.view.userInteractionEnabled=YES;
}
the animation is working well,and the application working well after the end of the animation. but when I doing animation with the next code:
动画运行良好,动画结束后应用程序运行良好。但是当我用下一个代码做动画时:
NSMutableArray *images=[NSMutableArray array]; //insrt the images dynamiclly
for(int i=1;i<=25;i++)
{
NSString *file=@"shavit_";
file=[file stringByAppendingFormat:@"%d",i];
NSString *filePath = [[NSBundle mainBundle] pathForResource:file ofType:@"png"];
[images addObject:[UIImage imageWithContentsOfFile:filePath]];
[file release];
[filePath release];
}
shavitimage.animationImages=images;
shavitimage.animationDuration=2.0;
shavitimage.animationRepeatCount=1;
[shavitimage startAnimating];
[images release]; //release the used array
[self performSelector:@selector(animation_ends) withObject:nil afterDelay:2.0];//after 2 seconds put astatic image
timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];
}
-(void)animation_ends{
[shavitimage stopAnimating];//the UIImageView return to static mode
shavitimage.image=[UIImage imageNamed:@"shavit_25.png"];
self.view.userInteractionEnabled=YES;
}
the animation is working well,but it stack after.why does it happen?
动画运行良好,但它会在之后叠加。为什么会发生这种情况?
回答by Deepak Danduprolu
Do not release an autoreleased object.
不要释放自动释放的对象。
That's the gist of it.
这就是它的要点。
Evaluation of chain of events :–
事件链的评估:-
- You create an autoreleased array.
Retain Count = 1 (will diminish by 1 in near future)
UIImageView
object retains the array when you setanimationImages
.Retain Count = 2
- You release the array.
Retain Count = 1
- Autorelease kicks in.
Retain Count = 0 (object is deallocated)
- 您创建一个自动发布的数组。
Retain Count = 1 (will diminish by 1 in near future)
UIImageView
对象在您设置时保留数组animationImages
。Retain Count = 2
- 你释放阵列。
Retain Count = 1
- 自动释放开始。
Retain Count = 0 (object is deallocated)
Now the UIImageView
still thinks that it has ownership of the array. When it tries to access it then it should result in an error. If it isn't animating and then is later deallocated, it will try to send a release
message to the deallocated instance which should also crash the application. All of this is fair assumptionas we don't know if the framework is adding its own memory management calls in any way.
现在UIImageView
仍然认为它拥有数组的所有权。当它尝试访问它时,它应该会导致错误。如果它没有动画,然后被释放,它会尝试向release
释放的实例发送一条消息,这也会使应用程序崩溃。所有这些都是合理的假设,因为我们不知道框架是否以任何方式添加了自己的内存管理调用。
Now both the pieces of code have the same problem and are both unreliable.
现在这两段代码都有同样的问题,都不可靠。
Be a good memory citizen and remove the release
call.
做一个良好的记忆公民并删除release
电话。
回答by Amol
@synthesize myImage;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *a = [NSMutableArray array];
//[a addObject: [UIViewController ]];
[a addObject:[UIImage imageNamed:@"1.gif"]];
[a addObject:[UIImage imageNamed:@"2.gif"]];
[a addObject:[UIImage imageNamed:@"3.gif"]];
[a addObject:[UIImage imageNamed:@"4.gif"]];
[a addObject:[UIImage imageNamed:@"5.gif"]];
[a addObject:[UIImage imageNamed:@"6.gif"]];
[a addObject:[UIImage imageNamed:@"7.gif"]];
//[myImage initWithFrame:CGRectMake(0, 0, 131, 125)];
myImage.animationImages = a;
myImage.animationDuration=5;
myImage.animationRepeatCount=0;
myImage.startAnimating;
[self.view addSubview:myImage];
}