iphone - 将UIView中的UIImageView对象添加到NSMutableArray中

标签 iphone ios arrays uiview nsmutablearray

我相信我可能正在处理一些 View 问题,这些问题不允许我检测 UIImageView 对象并将其添加到数组中。我确实需要一些建议。

我有许多 UIImageViews,其中的图像链接到位于 UIViewController 之上的 UIView(添加 UIView 是为了帮助使用 drawRect 和一些额外的优点)。因此,当我触摸图像并“拖放”它时,我想在拖放时将该图像添加到数组中。

我的 touchesBegan 获取触摸的位置并检查 UIImageView 是否被触摸,然后以该 View 为中心,如下所示:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

            UITouch *touch = [[event allTouches] anyObject];

            CGPoint location = [touch locationInView:self];  //can't use self.view in a UIView, this may be causing issues?

            startLocation = location;  // for reference as needed



    NSLog(@"Image x coord:   %f", location.x);

    NSLog(@"Image y coord:   %f", location.y);

    if ([touch view] == obiWan_Block)

            {obiWan_Block.center = location;}

    else if ([touch view] == r2d2_Block)

            {r2d2_Block.center = location;}

    else if ([touch view] == you_Block)

            {you_Block.center = location;}

}

然后,我使用 touchesMoved 拖动 UIImageView,最后使用 touchesEnded“放下”图像。当 UIImageView 放在屏幕的某个区域时,我将其“捕捉”到特定位置。此时我想将这个 UIImageView 放入一个数组中,但我没有运气。我相信我对所触及的各种 View 以及通过 addObject 添加到我的 NSMutableArray 的内容感到困惑。或者,我可能完全错过了一些东西。这是我的 touchedEnded 方法:

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

          UITouch *touch = [[event allTouches] anyObject];

          CGPoint location = [touch locationInView:self];

          UIView *temp = [touch view];

          UIImageView *currentImageView = (UIImageView *)[touch view];  //DON'T THINK THIS IS RIGHT

    NSLog(@"Current Image View is:   %@", currentImageView);

    if ((location.x > dropZone.origin.x) && (location.y >= dropZone.origin.y) && ([touch view] != NULL))

        {

            CGRect frame = temp.frame;

            if (touchCount == 0) {

                frame.origin.x = 15;

                frame.origin.y = 180;

                temp.frame = frame;

                [quoteArray addObject: currentImageView];

                touchCount++;

                }

            else if (touchCount == 1) {

                frame.origin.x = 70;

                frame.origin.y = 180;

                temp.frame = frame;

                [quoteArray addObject: currentImageView]; 

                touchCount++;

                }

                ....

我有一个 NSLog 语句来检查 addObject 是否正常工作,如下所示:

NSLog(@"the quote array contains %d items. Contents = %@",[quoteArray count], quoteArray);

日志总是显示:

the quote array contains 0 items. Contents = (null)

请多多指教,谢谢!

最佳答案

你的代码的最后一部分显示你有未初始化的quoteArray。创建代码时检查代码,我猜您在 init 方法中遗漏了某些内容。因为如果数组正确,那么 NSLog 应该显示如下:

NSArray *quoteArray = [NSArray array];
NSLog(@"%@", quoteArray); 

2011-11-17 17:37:00.506 TestApp[1382:207] ( )

关于iphone - 将UIView中的UIImageView对象添加到NSMutableArray中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8160086/

相关文章:

ios - 如何防止 UITableViewCell 被选中?

java - 在 json 数组中添加 json 对象,并在 sqlite 的 json 对象中添加 agian json 数组

iphone - 重置 Facebook 权限 iOS SDK

iphone - 在 Objective-C (iPhone) 中使用 SVC 网络服务

iphone - 获取响应映射对象的Json字符串 restkit v0.2.20

ios - 显示用户位置按钮 - Swift

ios - 有没有办法在 Swift 4 中使用 CGContext 创建没有指针的图像

java - 如何从文件中获取数据并替换数独游戏中的预定义数组

arrays - Array[Nothing with java.lang.Object] 在 Scala 2.9.1 中需要

iphone - 为标签添加阴影