2016-05-13 1 views
-1

UIScrollViewでimageViewsの配列をプログラムで作成し、ユーザにタップジェスチャを割り当てて対話させました。ここでは、どの画像ビューをクリックしてクリックした画像ビューから画像を取得できるかを区別したいと思います。しかし、私は彼らに別のタグを割り当てることができませんでした。ここに私のコードです。私は15画像のビューを追加しています。タグを使用してimageViewsの配列を割り当てて使用する方法

for (int i = 0; i < 15; i++) { 
     originx = ((_imageView.frame.size.width+5)*i); //Calculate origin x for each image view. 

    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(originx, self.view.frame.origin.y+5, imageViewHeightWidth, imageViewHeightWidth)]; 

    [_imageView setBackgroundColor:[ UIColor colorWithRed:191.0/255.0 green:65.0/255.0 blue:78.0/255.0 alpha:0.5]]; 
    //_imageView.layer.cornerRadius = 10.0; 
    _imageView.image = [UIImage imageNamed:[_imgArray objectAtIndex:i]]; 

    //Enabling user interaction for gesture. 
    [_imageView setUserInteractionEnabled:YES]; 
    [_imageView setMultipleTouchEnabled:YES]; 

    //Tap Gesture enabled. 
    _gesture =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTheImage:)]; 
    _gesture.numberOfTapsRequired = 1; 

    //Add a tap gesture. 
    [_imageView addGestureRecognizer:_gesture]; 

    //Assigning the tag to image View. 
    _imageView.tag = 200+i; 

    //Adding image in to scroll view. 
    [_gallaryScrollView addSubview:_imageView];  
} 

//Here i am getting same tag value for every imageView. i.e. "214". 
-(void)tapTheImage:(id)sender 
{ 
    NSLog(@"The tag value of imagView is%ld",(long)[_imageView tag]); 
} 
+0

にライン

_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(originx, self.view.frame.origin.y+5, imageViewHeightWidth, imageViewHeightWidth)]; 

を変更 私は質問のタグの重要性を理解していません。 'UITapGestureRecognizer'は、どのようなビューがタップされたのかを伝えるだけです。なぜ、タグが必要なのでしょうか?また、なぜローカル変数の代わりにインスタンス変数 '_imageView'を使用していますか? – trojanfoe

+0

この場合、タグを設定する必要はありません。あなたはrecognizer.viewによってimageviewを得ることができます! – Lion

答えて

3

変更するには、UIImageViewの新しいオブジェクトを作成していない

-(void) TapTheImage:(UITapGestureRecognizer *)gestureRecognizer{ 

    //Get the View 
    UIImageView *tableGridImage = (UIImageView*)gestureRecognizer.view; 
    NSLog(@"%d",tableGridImage.tag); 
} 
0

TapTheImage方法。

UIImageView *_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(originx, self.view.frame.origin.y+5, imageViewHeightWidth, imageViewHeightWidth)]; 
関連する問題