gpt4 book ai didi

ios - UIImageview 动画开始使用 for-in 循环和动画延迟

转载 作者:行者123 更新时间:2023-11-28 18:38:24 30 4
gpt4 key购买 nike

我需要为 UIimageview 制作动画,当我开始动画时,它会在很长一段时间后发生,我发现在为各种 ImageView 连续制作动画时,内存在某处泄漏。我需要做的就是在 ScrollView 中有 n 个 UIview,并且每个 View 都有一个自定义按钮。当选择该按钮时,会发生动画,如果动画正在进行中,如果用户点击按钮,动画将继续并且不需要重新开始

这是我的代码

 for (int i=0; i<AlphabetArray.count; i++) {

UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
view.backgroundColor=[UIColor grayColor];
view.tag=i+1;
[self.scrollView addSubview:view];
UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(200, 50, 400, 400)];


[image setImage:[[animationArray objectAtIndex:i]objectAtIndex:0]];
image.tag=i+1;
[view addSubview:image];

UIButton *animatebutton=[[UIButton alloc]initWithFrame:CGRectMake(200, 50, 336, 310)];
animatebutton.tag=i+1;
[animatebutton addTarget:self action:@selector(makeAnimation:) forControlEvents:UIControlEventTouchUpInside];
//[animatebutton setImage:[UIImage imageNamed:@"NewtDance_mov_0.png"] forState:UIControlStateNormal];
[view addSubview:animatebutton];
}

-(void)makeAnimation:(UIImageView *)sender {

UIView *tagView=(UIView *)[self.view viewWithTag:sender.tag];



for (UIImageView * imageview in [tagView subviews]) {

NSLog(@"Yes %d",imageview.tag);


if ([imageview isKindOfClass:[UIImageView class]]) {

if ([imageview isAnimating]) {
NSLog(@"Animation Happens");

}

else{
imageview.animationDuration=3.0;

imageview.animationImages=[animationArray objectAtIndex:sender.tag-1];
imageview.animationRepeatCount=2;
imageview.tag=sender.tag;
[imageview startAnimating];
}
}

}



NSLog(@"OK");


}

通过这段代码,我可以实现我的要求。但是由于使用了 for-in 循环,动画开始的很晚

最佳答案

处理此问题的最佳方法。使用 UIImageViewanimationImages 属性。

  1. 使用图像的 UIImage 对象创建 NSMutableArray 对象。
  2. 通过
    将图像数组设置为UIImageView animationImages 属性[imageView setAnimationImages:imagesArray]

  3. [imageView startAnimating] 开始动画。

  4. [imageView stopAnimating] 停止动画。
    查看文档以了解更多详细信息。

更多详情,check this. .

关于ios - UIImageview 动画开始使用 for-in 循环和动画延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15022449/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com