2011-04-27 21 views
0

我有3 * 3的圖像從一個圖像(通過裁剪),但如何管理這些imageView的移動,我必須聲明所有這些或任何其他方式的不同對象是他們......有可能使用數組管理這些太多的圖像....請幫助好友玩UIImageView?

我們可以通過對象數組來完成...?

- (void)viewDidLoad { 

UIImage *img = [UIImage imageNamed:@"3.jpg"]; 

for(int i = 0;i < 3;i++) 
{ 
    for (int j = 0; j < 3 ; j++) 

    [self SetImagePeces:i :j]; 

} 
    [super viewDidLoad]; 
} 


-(void) SetImagePeces:(int)col:(int)row 
{ 

UIImage *img = [UIImage imageNamed:@"3.jpg"]; 

img1 = [[UIImageView alloc] init]; 
img1.frame = CGRectMake(0 + 235 * col, 0 + 235 * row, 233, 233); 
img1.tag = row; 

img1.userInteractionEnabled = YES; 

CGRect clippedRect = CGRectMake(0 + 233 * col,700 - 233 * row, 233, 233); 

UIImage *imgV = [self imageByCropping:img toRect:clippedRect]; 

img1.image = imgV; 

[self.view addSubview:img1]; 
[img1 release]; 


} 

更新::

不過現在我嘗試在700 * 700大小的圖片,但如何將圖像的大小我們任何規模大小約束..

**觸摸事件:: **

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

UITouch *touch = [touches anyObject]; 
    if (touch.view == img2) { 

      if (c2 == 0) { 
        tempImg = img2.image; 
        [flag replaceObjectAtIndex:1 withObject:@"1"]; 
        c2++; 
      } 
      else { 
        for(int j=0;j<9;j++) 
        { 
          if(j==1) 
            {[flag replaceObjectAtIndex:j withObject:@"0"];} 
          else 
          { 
            int a=[[flag objectAtIndex:j]integerValue]; 
          if (a==1) { 
            img3.image=img2.image; 
            [flag replaceObjectAtIndex:j withObject:@"0"]; 
          } 
          } 
        } 
        img2.image = tempImg; 
        c2 = 0; 
      } 
    } 
    if (touch.view == img3) { 

      if (c2 == 0) { 
        tempImg = img3.image; 
        [flag replaceObjectAtIndex:2 withObject:@"1"]; 
        c2++; 
      } 
      else { 
        for(int j=0;j<9;j++) 
        { 
          if(j==2) 
          {[flag replaceObjectAtIndex:j withObject:@"0"];} 
          else 
          { 
          int a=[[flag objectAtIndex:j]integerValue]; 
          if (a==1) { 
            img2.image=img3.image; 
            [flag replaceObjectAtIndex:j withObject:@"0"]; 
          } 
          } 
        } 
        img3.image = tempImg; 
        c2 = 0; 
      } 
    } 


} 

這裏我換觸摸圖像開始方法,以便僅實現2幅圖像,但實在是太困難的奶源更多圖片

Thanx預先

回答

1

我將使用NSMutableDictionary來管理這些對象。您可以按如下方式定義字典的關鍵字。然後,您可以使用行號和列號訪問所需圖像。

- (NSString *)keyForRow:(int)row col:(int)col { 
    return [NSString stringWithFormat:@"%d_%d", row, col]; 
} 

UPDATE

的NSMutableDictionary * myImageDict = ...; /// <一個字典來管理UIImageView。

- (void)setImageView:(UIImageView *)image row:(int)row col:(int)col 
    { 
     [myImageDict setObject:image forKey:[self keyForRow:row col:col]]; 
    } 

- (UIImageView *)imageWithRow:(int)row col:(int)col 
{ 
    return [myImageDict objectForKey:[self keyForRow:row col:col]]; 
} 

然後,您可以通過NSMutableDictionary使用行號和列號管理UIImageView。

關於如何移動這些UIImageView,可以使用UIGestureRecognizer監視UIImageView上的觸摸事件。通過處理那些觸摸事件,特別是touchedMoved:withEvent:和修改UIImageView的中心,您可以根據需要移動這些UIImagesView。

+0

我上面添加了我的代碼pls ...給我建議...我必須隨機設置圖像比觸摸重新排列它... – AJPatel 2011-04-27 13:44:49

+0

謝謝你哥們我試試這個,直到現在我使用單獨的UIImageview對象和使用觸摸事件。讓我們看看可能對我有用...... – AJPatel 2011-04-28 08:39:51

+0

不客氣。 :) – AechoLiu 2011-05-30 14:06:12

1

你需要採取多個uiimageview對象,touchmove移動感動的imageview的中心。

+0

亞朋友我做的方式完全相同,但在這裏我只涉及3 * 3(9 imageView obj),但是當矩陣大小在那個時候增加很難管理這太多的對象和內存manegment它也太...,我還添加我的Touch事件代碼看看.. – AJPatel 2011-04-28 10:36:47