【问题标题】:Swipe Gesture Not Working滑动手势不起作用
【发布时间】:2013-05-15 09:02:02
【问题描述】:

我想滑动我的图像视图,但没有切换到左侧。 我从前卡数组中获取图像。我想在 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。 如何解决我的问题帮助我解决这个问题。 提前致谢。

【问题讨论】:

  • 控制台是否显示来自 leftSwipeHandle: 方法的日志?
  • 是否调用了leftSwipeHandle 方法,你检查断点了吗
  • UIImageView 默认禁用 userInteraction,你需要明确设置为 YES。
  • 我做了所有这些事情,但图像不会显示。并调用了我的 leftSwipeHandle 方法。
  • @JitendraDeore :leftSwipeHandle 中的 for 循环有什么用。数组中有 9 个元素,并且您使用随机数作为图像,因此最后一张图像将显示在图像视图中。所以我认为不需要那个 for 循环。

标签: ios objective-c uiswipegesturerecognizer


【解决方案1】:

尝试使用这个。因为默认图像的用户交互是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);
    }
}

【讨论】:

  • ImgView.userInteractionEnabled = YES;在 viewDidLoad 中写入这一行而不是在这个方法中 - (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer。并查看我编辑的答案
  • 图像左交换时不显示。
  • 签入for循环。你得到什么图像名称?
  • 不,我的意思是你从这行得到的 [delegate.FrontsCards objectAtIndex:randIdx]
  • 图片显示在nslog但imageview只显示一张图片不刷新图片
【解决方案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;
    }
}

【讨论】:

  • 答案的第一部分可能是有效的,但是使用imgCount是无效的,它消除了随机性。
  • 是的,但不需要在问题中提到的 leftSwipeHandle 方法中编写 for 循环。所以,我写了这段代码......如果这是可行的,那么我修改一些代码......这是为了检查图像是否改变......
  • cloub1.png, cloub2.png 所有图片是否存在于资源中?
  • 所有图片都在资源中。
  • NSLog(@"%@",[delegate.FrontsCards objectAtIndex:imgCount]);你能给出这条线的输出吗?
【解决方案3】:

试试这个

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

    • 打印图片名称 [delegate.FrontsCards objectAtIndex:randIdx] 是正确的还是空白的?
    • 打印什么?..[delegate.FrontsCards objectAtIndex:randIdx]
    【解决方案5】:

    试试这个。

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

    UIImageView 默认禁用。您需要启用它。

    【讨论】:

      猜你喜欢
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多