OGeek|极客世界-中国程序员成长平台

标题: ios - IOS中的滑动手势识别 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:59
标题: ios - IOS中的滑动手势识别

我是 iOS 新手。

我正在开发一个有 2 个 NSMutableArray 的应用程序。我想将数组图像加载到 ImageView

我想拍摄第一张图片并将其添加到 ImageView。当使用向左滑动时,我想将数组中的下一个图像添加到新的 ImageView 并显示它。如果他们向后滑动,请加载以前的 e.t.c

当用户双击图像时,我还想将第二个数组中的图像加载到 ImageView 中。

我试过这个

AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    NSLog(@"%lu",(unsigned long)[delegate.FrontsCards count]);
    ImgView= [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

    //ImgView.image=[UIImage imageNamed"cloub1.png"];

    int random=arc4random()%[delegate.FrontsCards count];
     NSLog(@"%d",random);

    ImgView.image=[delegate.FrontsCards objectAtIndex:random];
    [self.view addSubview:ImgView];
    currentImageIndex = 0;
    ImgView.userInteractionEnabled = YES;

    UISwipeGestureRecognizer *recognizer;

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self actionselector(handleSwipeFrom];
    [recognizer setDirectionUISwipeGestureRecognizerDirectionLeft)];
    [[self view] addGestureRecognizer:recognizer];

    [super viewDidLoad];

}

-(void)handleSwipeFromUISwipeGestureRecognizer *)recognizer
{
    NSLog(@"Swipe received.");

    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    for(int i=0;i<52;i++)
    {
        NSLog(@"%d",i);
        ImgView.image=[delegate.FrontsCards objectAtIndex:i];
    }

    ImgView.image=[UIImage imageNamed"cloub3.png"];
}

谁能帮我做这件事?提前谢谢你。



Best Answer-推荐答案


几种方法:

  1. 滑动手势:如果你想编写自己的滑动手势,你可以这样做:

    • 不仅定义图像名称数组的属性,还定义一个索引,显示您正在查看的卡片:

      @property (nonatomic, strong) NSMutableArray *imageNames;
      @property (nonatomic) NSInteger imageIndex;
      
    • 将手势添加到您的 ImageView 中:

      UISwipeGestureRecognizer *swipe;
      
      // by the way, if not using ARC, make sure to add `autorelease` to
      // the following alloc/init statements
      
      swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self actionselector(handleSwipe];
      swipe.direction = UISwipeGestureRecognizerDirectionLeft;
      [self.imageView addGestureRecognizer:swipe];
      
      swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self actionselector(handleSwipe];
      swipe.direction = UISwipeGestureRecognizerDirectionRight;
      [self.imageView addGestureRecognizer:swipe];
      
    • 编写手势识别器处理程序:

      - (void)handleSwipeUISwipeGestureRecognizer *)gesture
      {
          UIViewAnimationOptions option = kNilOptions;
      
          if (gesture.direction == UISwipeGestureRecognizerDirectionRight)
          {
              // if we're at the first card, don't do anything
      
              if (self.imageIndex == 0)
                  return;
      
              // if swiping to the right, maybe it's like putting a card
              // back on the top of the deck of cards
      
              option = UIViewAnimationOptionTransitionCurlDown;
      
              // adjust the index of the next card to be shown
      
              self.imageIndex--;
          }
          else if (gesture.direction == UISwipeGestureRecognizerDirectionLeft)
          {
              // if we're at the last card, don't do anything
      
              if (self.imageIndex == ([self.imageNames count] - 1))
                  return;
      
              // if swiping to the left, it's like pulling a card off the 
              // top of the deck of cards
      
              option = UIViewAnimationOptionTransitionCurlUp;
      
              // adjust the index of the next card to be shown
      
              self.imageIndex++;
          }
      
          // now animate going to the next card; the view you apply the 
          // animation to is the container view that is holding the image
          // view. In this example, I have the image view on the main view,
          // but if you want to constrain the animation to only a portion of
          // the screen, you'd define a simple `UIView` that is the dimensions
          // that you want to animate and then put the image view inside
          // that view, and replace the `self.view` reference below with the
          // view that contains the image view.
      
          [UIView transitionWithView:self.view
                            duration:0.5
                             optionsption
                          animations:^{
                              self.imageView.image = [UIImage imageWithContentsOfFile:self.imageNames[self.imageIndex]];
                          }
                          completion:nil];
      }
      
  2. 显然,如果您想编写自己的双击手势,请继续为此创建一个手势以及该手势的处理程序,例如:

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self actionselector(handleTap];
    tap.numberOfTapsRequired = 2;
    [self.imageView addGestureRecognizer:tap];
    
  3. 正如其他人所建议的,如果您不想像上面那样编写自己的图像处理代码,您可以使用各种其他控件,例如UIPageViewControllerUICollectionView 或其他十几个便于在一组图像之间导航的类中的任何一个。

关于ios - IOS中的滑动手势识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16607898/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4