Question

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.

Was it helpful?

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);
    }
}

OTHER TIPS

in .h file

int imgCount;

in .m file

- (void)viewDidLoad  {
    [super viewDidLoad];

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

    imgCount = 0;
}

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer {
    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    ImgView.image=[UIImage imageNamed:[delegate.FrontsCards objectAtIndex:imgCount]];
    imgCount++;
    if (imgCount >= 9) {
       imgCount = 0;
    }
}

Try this

[ImgView addGestureRecognizer:leftRecognizer];
//Add this
ImgView.userInteractionEnabled = YES;

You need to set imageview.userInteractionEnabled = YES; on UIImageView.

Try this.

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

UIImageView is disabled by default. You need to make it enabled.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top