• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - IOS中的滑动手势识别

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:59:01 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我是 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap