滑动手势不起作用

时间:2013-05-15 09:02:02

标签: ios objective-c uiswipegesturerecognizer

我想刷我的图片视图,但没有交换到左侧。 我从前卡阵列获取图像。我想在imageview中显示并轻扫一下。

我试过这个

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。 如何解决我的问题帮助我解决这个问题。 提前谢谢。

5 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分: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;
    }
}

答案 2 :(得分:1)

试试这个

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

试试这个。

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

UIImageView处于禁用状态。你需要启用它。