我相信我可能正在處理一些視圖問題,這些問題不允許我檢測並將UIImageView
對象添加到數組。我真的可以使用一些建議。將UIImage中的UIImageView對象添加到NSMutableArray
我已經得到了一些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;}
}
然後,我拖動UIImageView
周圍touchesMoved
最後,「降」的圖片與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);
日誌總是說:
報價數組包含0項。內容=(空)
請指教,謝謝!
謝謝!我確實在錯誤的地方初始化了我的'NSMutableArray'。總是需要數小時才能解決的簡單事情! :-) – jeffjohn01