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

标签 ios objective-c xcode uiimageview uianimation

我需要为 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/

相关文章:

ios - 如何为辅助 Today Center 小部件文本指定鲜艳的外观?

ios - 使用 imageWithContentsOfFile : 从 XCAssets 获取启动图像

swift - 在 Swift 3 中重复 Process() n 次

ios - 检查 Cordova iOS 构建中的依赖项错误

ios - 制作 UIApplication(AppDelegate) 嵌入对象

ios - NSCopying 协议(protocol)不工作

ios - UIButton 单击发件人总是转到选定的选项

ios - 带有 UIRectCornerBottomLeft/Right 的 CAShapeLayer 使我的 View 消失

iphone - Facebook SDK 3.0 与 iOS 5 和 Xcode 4.2

ios - AWS S3 公共(public)对象与私有(private)对象?