Iphone 将UIView中的UIImageView对象添加到NSMutableArray
我相信我可能正在处理一些视图问题,这些问题不允许我检测并向数组中添加Iphone 将UIView中的UIImageView对象添加到NSMutableArray,iphone,ios,arrays,uiview,nsmutablearray,Iphone,Ios,Arrays,Uiview,Nsmutablearray,我相信我可能正在处理一些视图问题,这些问题不允许我检测并向数组中添加UIImageView对象。我真的需要一些建议 我有许多uiimageview,其中图像链接到UIView,该视图位于UIViewController之上(添加UIView有助于drawRect和一些其他优点)。所以,当我触摸一个图像并“拖放”它时,我想在拖放时将该图像添加到数组中 MyTouchesBegind获取触摸的位置,并检查正在触摸的UIImageView,然后以该视图为中心,如下所示: -(void) touches
UIImageView
对象。我真的需要一些建议
我有许多uiimageview
,其中图像链接到UIView
,该视图位于UIViewController
之上(添加UIView
有助于drawRect
和一些其他优点)。所以,当我触摸一个图像并“拖放”它时,我想在拖放时将该图像添加到数组中
MyTouchesBegind
获取触摸的位置,并检查正在触摸的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;}
}
NSLog(@"the quote array contains %d items. Contents = %@",[quoteArray count], quoteArray);
然后,我用touchesMoved
拖动UIImageView
,最后用touchesend
拖动图像。当UIImageView
被放置在屏幕的某个区域时,我会将其“捕捉”到特定位置。在这一点上,我想把这个UIImageView
放到一个数组中,但我没有运气。我相信我对所涉及的各种视图以及通过addObject
添加到我的NSMutableArray
中的内容感到困惑。或者,我可能完全错过了什么。这是我的toucheeded
方法:
-(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
是否按如下方式工作:
-(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;}
}
NSLog(@"the quote array contains %d items. Contents = %@",[quoteArray count], quoteArray);
日志上总是说:
quote数组包含0项。内容=(空)
请指教,谢谢 代码的最后一部分显示您未初始化
quotarray
。在创建代码时检查代码,我想您在init方法中遗漏了一些东西。因为如果阵列正确,则NSLog应显示如下:
NSArray *quoteArray = [NSArray array];
NSLog(@"%@", quoteArray);
2011-11-17 17:37:00.506 TestApp[1382:207]()
谢谢我确实在错误的位置初始化了我的
NSMutableArray
。总是简单的事情需要花上几个小时才能解决!:-)