ios - UIImageView 和 UIVIew 动画

标签 ios objective-c uiview uiimageview

我需要创建从机器人到顶部的移动图像,具有不同的移动速度和大小。 我写了这个:

NSInteger random=arc4random()%10;
random += 10;
for (NSInteger curImage = 0 ; curImage < random; curImage++)
{

    UIImageView *tmpImage = [[UIImageView alloc]  initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ball" ofType:@"png"]]];
    [self addSubview:tmpImage];
    [self sendSubviewToBack:tmpImage];
    tmpImage.tag = imageTag;
    NSInteger sizeImage  = arc4random()%4;
    sizeImage += 1;
    tmpImage.frame  = CGRectMake(0.0,0.0, sizeImage * 10.0, sizeImage * 10.0);
    
    tmpImage.contentMode = UIViewContentModeScaleToFill;
    NSInteger xStartPosition = arc4random()%1024, yStartPosition = arc4random()%748;
    tmpImage.center = CGPointMake( xStartPosition , yStartPosition + self.frame.size.height);
    [UIView animateWithDuration:6.0f
                     animations:^{
                         [tmpImage setFrame:CGRectMake(tmpImage.frame.origin.x, - tmpImage.image.size.height - self.frame.size.height, tmpImage.frame.size.width, tmpImage.frame.size.height)];
                     }
                     completion:^(BOOL finished){
                     }
     ];
}

删除我使用:

for (UIImageView *img in [self subviews]) {
        if (img.tag==imageTag) {
            if (img.frame.origin.y > 0 ) {
                [img removeFromSuperview];
            }
        }
    }

但是我的应用程序崩溃了 Exited: Killed: 9

我可以用另一种方式实现吗?有什么想法吗???

感谢帮助!!!

编辑崩溃列表:

<Warning>: Application 'UIKitApplication:Name.Name' exited abnormally with signal 9: Killed: 9

 error: ::read ( 5, 0x1df9fc, 18446744069414585344 ) => -1 err = Bad file descriptor (0x00000009)

libMobileGestalt copySystemVersionDictionaryValue: Could not lookup ReleaseType from system version dictionary

最佳答案

最好启用断点并检查崩溃的位置。

将异常断点添加到您的 xcode 中:

https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html

但我猜你是在迭代数组的同时修改它。在通过 [self subviews] 时,您可以在调用 [img removeFromSuperView] 时间接修改它。

试试这个:

NSMutableArray* toRemove = [NSMutableArray arrray];
for (UIImageView *img in [self subviews]) {
    if (img.tag==imageTag) {
        if (img.frame.origin.y > 0 ) {
        [toRemove addObject:img];
        }
    }
}

for (UIImageView *img in toRemove) {
    [img removeFromSuperview];
}

关于ios - UIImageView 和 UIVIew 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18717378/

相关文章:

ios - 如何格式化将字符串2013-01-27T02:31:47 + 08:00转换为NSDate

iOS Storyboard : Views outside of the ViewController and on Top of the Scene (between First Responder and Exit boxes)

ios - 隐藏并删除嵌套 UIView swift 的所有空间

ios - Xcode - 在同一个图像中拖了两次,它被列出了两次,为什么?删除其中一张图片有风险吗?

iphone - keysSortedByValueUsingSelector 崩溃但 sortedArrayUsingSelector 运行正常

objective-c - Uncrustify ObjC 换行符后的方法

iphone - UIActionSheet 按钮显示 View

ios - 添加场景到director cocos2d iOS

ios - 来自 AVDepthData 的深度图不同于 Photoshop 中的 HEIC 文件深度数据

iphone - iPhone SDK 如何在通讯录中创建联系人?