-1

Code:

Header file:

@interface game : UIViewController
{

    UIImageView *anh[8][8];

}

-(void)SwipeToMove:(id)sender;

@end

Implementation file:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UISwipeGestureRecognizer *move = [[UISwipeGestureRecognizer    alloc]initWithTarget:self action:@selector(SwipeToMove:)];
   [move setDirection:(UISwipeGestureRecognizerDirectionUp)];

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

            anh[i][j]=[[UIImageView alloc] initWithFrame:CGRectMake(0+40*i,200 + 40*j,40,40)];

            anh[i][j].userInteractionEnabled = YES;

            [self.view addSubview:anh[i][j]];

            anh[i][j].image = [UIImage imageNamed:@"Earth.png"];

            [anh[i][j] addGestureRecognizer:move];

       }
   }

}

-(void)SwipeToMove:(id)sender{

    NSLog(@"ok");
}

The UIImageView works as expected, but UISwipeGestureRecognizer is not working. Then, I tried this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(100,50,40,40)];
    label1.image = [UIImage imageNamed:@"Ceres.png"];
    [self.view addSubview:image1];
    image1.userInteractionEnabled = YES;
    image1.image = [UIImage imageNamed:@"Earth.png"];

    UISwipeGestureRecognizer *move = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipeToMove:)];
   [move setDirection:(UISwipeGestureRecognizerDirectionUp)];

    [image1 addGestureRecognizer:move];



-(void)SwipeToMove:(id)sender{

    NSLog(@"OK");

}

It's working, but I need an array of UIImageViews to make my app. Please help me.

duci9y
  • 4,128
  • 3
  • 26
  • 42

2 Answers2

0

Your code is only creating one gesture recognizer, but you try to add it many image views. You need to create the gesture recognizer inside the loop so you create a new one for each image view.

 (void)viewDidLoad
{
    [super viewDidLoad];

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            UISwipeGestureRecognizer *move = [[UISwipeGestureRecognizer    alloc]initWithTarget:self action:@selector(SwipeToMove:)];
            [move setDirection:(UISwipeGestureRecognizerDirectionUp)];
            anh[i][j]=[[UIImageView alloc] initWithFrame:CGRectMake(0+40*i,200 + 40*j,40,40)];

            anh[i][j].userInteractionEnabled = YES;

            [self.view addSubview:anh[i][j]];

            anh[i][j].image = [UIImage imageNamed:@"Earth.png"];

            [anh[i][j] addGestureRecognizer:move];

       }
   }

}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • thank you very much for your answer. i will try it, but i have a question. in this case, dont i need a array of UISwipeGestureRecognizer ? – Dang Nguyen Huu Jul 25 '14 at 04:48
  • @DangNguyenHuu, no, you don't need an array of the gesture recognizers, since each view will have their own. If you want to know which view was swiped, you can get that from the gesture recognizer's view property. – rdelmar Jul 25 '14 at 05:23
  • Thank you for a commentary. You've been a big help. – Dang Nguyen Huu Jul 25 '14 at 07:34
0

You are adding the same gesture recognizer all the time to the different images, so it will take just the last one for my understanding, because you have just created one. Create an aux recognizer like an aux var inside the loop and give a tag to every UIImageView so you can access to the tag and distinguish every swipe in every image, when it has happened in your app. It should work. But basically :

for (....){
   // create aux gesture recognizer and assign it to the UIImageView
}

Hope it helps.

AlfuryDB
  • 289
  • 1
  • 4