2

我是 iOS 新手。

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

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

我还想将第二个数组中的图像加载到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 action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [[self view] addGestureRecognizer:recognizer];

    [super viewDidLoad];

}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)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"];
}

谁能帮我做这件事?先感谢您。

4

3 回答 3

1

首先,您是否只为 iOS 6 及更高版本构建应用程序?如果是,您可以使用UICollectionView启用分页并且一次只能看到一个项目。博客文章“使用 UICollectionView 创建分页照片库”解释了如何执行此操作。

如果您正在为 iOS 6 以下构建应用程序,您可以找到类似的自定义网格视图来替换 UICollectionView,如GMGridViewAQGridView。另一种选择是构建一个自定义 UIScrollView,其中包含 3 个 UIImageView,然后当用户滚动到列表末尾时相应地调整滚动视图偏移量。但是,像其他现有的开源网格视图或 UICollectionView 一样优化自定义 UIScrollView 需要一些时间。

对于双击,您可以UITapGestureRecognizernumberOfTapsRequired = 2UICollectionView 或网格视图或自定义滚动视图中使用。一旦发生双击,计算在哪个索引路径上发生了点击(使用indexPathForItemAtPoint:of UICollectionView,或indexForItemAtPoint:of AQGridView,或任何其他类似方式)。获得索引后,您可以使用第二个数组做您想做的事情。

于 2013-05-17T13:51:09.883 回答
1

几种方法:

  1. 滑动手势:如果您想编写自己的滑动手势,您可以执行以下操作:

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

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

      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 action:@selector(handleSwipe:)];
      swipe.direction = UISwipeGestureRecognizerDirectionLeft;
      [self.imageView addGestureRecognizer:swipe];
      
      swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
      swipe.direction = UISwipeGestureRecognizerDirectionRight;
      [self.imageView addGestureRecognizer:swipe];
      
    • 编写手势识别器处理程序:

      - (void)handleSwipe:(UISwipeGestureRecognizer *)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
                             options:option
                          animations:^{
                              self.imageView.image = [UIImage imageWithContentsOfFile:self.imageNames[self.imageIndex]];
                          }
                          completion:nil];
      }
      
  2. 显然,如果您想编写自己的双击手势,请继续为此创建一个手势,以及该手势的处理程序,例如:

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

于 2013-05-17T14:58:00.413 回答
-1

单程:

您需要在 imageView 上放置一个滑动手势识别器,当您收到回调时,从数组中选择正确的图像。

UISwipeGestureRecognizer *g = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetected:)];

--

“更好的 IMO 将是一个滚动视图——它可以让你在滑动时获得动画”

于 2013-05-17T13:19:33.027 回答