2012-03-09 49 views
0

我看任何方法如何可能觸摸圖像和改變你的位置。但同樣的事情需要在屏幕上顯示20個圖像。所以我用不同的標籤生成20個UIimageViews。但我仍然不知道如何通過標記選擇這些UIimageViews。如何通過標籤選擇和移動UIimageViews?

有我的代碼:

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

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:self.view]; 

    imageView = (UIImageView *)[imageView viewWithTag:imageView.tag] ; 
    NSLog(@"%d",imageView.tag); 
    imageView.center = location; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    imageView.tag = arc4random() % 19; 
} 

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

} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    imageView = nil; 

    for (int i; i<19; i++) { 

     int randX = arc4random() % 240; 
     int randY = arc4random() % 368; 

     imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_1.png"]]; 
     imageView.frame = CGRectMake(randX, randY, 80, 92); 
     imageView.tag = i; 

     //NSLog(@"%d",imageView.tag); 

     [self.view addSubview:imageView]; 
    } 
} 

感謝您閱讀

+0

「viewWithTag」有什麼問題? – Saphrosit 2012-03-09 14:06:35

回答

0

如果您有標籤的列表中,這樣的事情:

#define TAG_VIEW1 1 
#define TAG_VIEW2 2 

您可以輕鬆地做到:

[self.view viewWithTag:TAG_VIEW1]; 

那個方法會讓你回到那個標籤的視圖。