Swipegesture在IOS中识别

时间:2013-05-17 11:30:13

标签: ios objective-c uiimageview uiswipegesturerecognizer

我是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 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"];
}

有人可以帮我这么做吗?提前谢谢。

3 个答案:

答案 0 :(得分:1)

首先,您是否仅为iOS 6及更高版本构建应用程序?如果是,您可以使用UICollectionView启用分页,一次只能看到一个项目。博文"Creating a Paged Photo Gallery With a UICollectionView"解释了如何做到这一点。

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

对于双击,您可以在UICollectionView或网格视图或自定义滚动视图中使用UITapGestureRecognizer numberOfTapsRequired = 2。双击发生后,计算点击发生的索引路径(使用indexPathForItemAtPoint: UICollectionViewindexForItemAtPoint: AQGridView或任何其他类似方式)。获得索引后,您可以使用第二个数组执行所需的操作。

答案 1 :(得分: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. 正如其他人所建议的那样,如果你不想像上面那样编写自己的图像处理代码,你可以使用各种其他控件,例如:一个UIPageViewController,一个UICollectionView或其他类别中的任何一个,可以促进一组图像之间的导航。

答案 2 :(得分:-1)

单步:

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

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

-

'更好的IMO将是一个滚动视图 - 当你滑动'

时会获得动画