2

我想滑动我的图像视图,但没有切换到左侧。我从前卡数组中获取图像。我想在图像视图中显示并垂直滑动。

我试过这个

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

图像未交换到 imageview。如何解决我的问题帮助我解决这个问题。提前致谢。

4

5 回答 5

3

尝试使用这个。因为默认情况下图像的用户交互是NO. 所以你需要设置用户交互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);
    }
}
于 2013-05-15T09:09:51.147 回答
2

在 .h 文件中

int imgCount;

在 .m 文件中

- (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;
    }
}
于 2013-05-15T09:18:21.630 回答
1

尝试这个

[ImgView addGestureRecognizer:leftRecognizer];
//Add this
ImgView.userInteractionEnabled = YES;
于 2013-05-15T09:10:38.297 回答
0

您需要设置imageview.userInteractionEnabled = YES;UIImageView.

于 2013-05-15T09:08:20.643 回答
0

尝试这个。

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

UIImageView默认情况下禁用。您需要启用它。

于 2013-05-15T09:27:54.730 回答