Search code examples
iosobjective-cuiswipegesturerecognizer

Swipe Gesture Not Working


i wanted to swipe my image view but not swapped to left side. i get image from frontcard array. and i wanted to display in imageview and swipe verticlally.

I tried this one

FrontsCards =[[NSMutableArray alloc]initWithCapacity:9];
[FrontsCards insertObject:@"cloub1.png" atIndex:0];
[FrontsCards insertObject:@"cloub2.png" atIndex:1];
[FrontsCards insertObject:@"cloub3.png" atIndex:2];
[FrontsCards insertObject:@"cloub4.png" atIndex:3];
[FrontsCards insertObject:@"cloub5.png" atIndex:4];
[FrontsCards insertObject:@"cloub6.png" atIndex:5];
[FrontsCards insertObject:@"cloub7.png" atIndex:6];
[FrontsCards insertObject:@"cloub8.png" atIndex:7];
[FrontsCards insertObject:@"cloub9.png" atIndex:8];

- (void)viewDidLoad {
    [super viewDidLoad];

    UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
    leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [leftRecognizer setNumberOfTouchesRequired:1];
    [ImgView addGestureRecognizer:leftRecognizer];
}

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
    int m = 0;
    NSLog(@"Left Swipe");

    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    NSLog(@"%d",m);

    for(m=0; m<[delegate.FrontsCards count];m++) {
        int randIdx=arc4random()%[delegate.FrontsCards count];   // Randomly     shufffled
        ImgView.userInteractionEnabled=YES;
        ImgView.image=[UIImage imageNamed:[delegate.FrontsCards objectAtIndex:randIdx]];

        NSLog(@"%d",m);
    }
}

images not swapped to imageview. how to solve my problem help me out this. thanks in advance.


Solution

  • Try to use this one. Because by default image's user interaction is NO. So you need to set user interaction YES.

    FrontsCards =[[NSMutableArray alloc]initWithCapacity:9];
    [FrontsCards insertObject:@"cloub1.png" atIndex:0];
    [FrontsCards insertObject:@"cloub2.png" atIndex:1];
    [FrontsCards insertObject:@"cloub3.png" atIndex:2];
    [FrontsCards insertObject:@"cloub4.png" atIndex:3];
    [FrontsCards insertObject:@"cloub5.png" atIndex:4];
    [FrontsCards insertObject:@"cloub6.png" atIndex:5];
    [FrontsCards insertObject:@"cloub7.png" atIndex:6];
    [FrontsCards insertObject:@"cloub8.png" atIndex:7];
    [FrontsCards insertObject:@"cloub9.png" atIndex:8];
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
        leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
        [leftRecognizer setNumberOfTouchesRequired:1];
        [ImgView addGestureRecognizer:leftRecognizer];
    
        // --------------- Add this line here ------------
        ImgView.userInteractionEnabled = YES;
    }
    
    - (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer {
        int m = 0;
        NSLog(@"Left Swipe");
    
        AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
        NSLog(@"%d",m);
    
        for(m=0; m<[delegate.FrontsCards count];m++) {
            int randIdx=arc4random()%[delegate.FrontsCards count];   // Randomly     shufffled
            ImgView.image=[UIImage imageNamed:[delegate.FrontsCards objectAtIndex:randIdx]];
            NSLog(@"%d",m);
        }
    }