繁体   English   中英

将UIView中的UIImageView对象添加到NSMutableArray

[英]Adding UIImageView Objects in UIView to NSMutableArray

我相信我可能正在处理一些视图问题,这些问题不允许我检测UIImageView对象并将其添加到数组。 我真的可以使用一些建议。

我有许多UIImageViews与链接到图片UIView是在上面坐UIViewController (的UIView加入帮助drawRect和一些额外的好处)。 因此,当我触摸图像并“拖放”它时,我想在拖放图像时将该图像添加到数组中。

我的touchesBegan获取触摸的位置并检查UIImageView是否被触摸,然后在该视图上居中,如下所示:

-(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放入一个数组中,但是我没有运气。 我相信我对所涉及的各种视图以及通过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);

日志总是显示:

quote数组包含0个项目。 内容=(空)

请指教,谢谢!

代码的最后一部分显示您quoteArray初始化quoteArray 创建代码时检查代码,我猜您在init方法中错过了一些东西。 因为如果数组正确,那么NSLog应该显示如下:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM