2012-03-09 4 views
0

私はどのような方法を見ても、画像に触れることができ、あなたの位置を変えることができます。しかし、同じことが画面上に20枚の画像が必要です。だから私は別のタグ20 UIimageViewsを生成します。しかし、私はまだこれらのUIimageViewsタグをどのように選択するか分からない。タグでUIimageViewsを選択して移動するにはどうすればいいですか?

私のコードがある:

#define TAG_VIEW1 1 
#define TAG_VIEW2 2 

あなたは簡単に行うことができます:

- (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

答えて

0

をお読みいただき、ありがとうございまし

[self.view viewWithTag:TAG_VIEW1]; 

そしてそのメソッドは、そのタグのビューを返します。

関連する問題