如何在详细视图UIimage中为右/左创建滑动手势

时间:2016-03-04 05:05:58

标签: ios objective-c uitableview uiimage uiswipegesturerecognizer

我正在开发一个食品应用程序,其中每个tableViewCell显示不同的食谱名称。当用户点击配方(在单元格中)时,将打开详细视图。

我现在正在尝试为UIImageView实施滑动手势,该手势会在滑动时显示不同的图像。我该如何实现?

 UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];

 UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];


 // Setting the swipe direction.
 [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
 [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
 // Adding the swipe gesture on image view

 [Allimages addGestureRecognizer:swipeLeft];
 [Allimages addGestureRecognizer:swipeRight];



- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe{

 NSInteger indexPath;

 if (indexPath==0) {

 if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {


   arrayimage=[[NSMutableArray alloc]initWithObjects:@ "BBQ Chicken Pizza.jpg",@ "roastchickensteaks.jpg", nil];


   swipee =  (swipee > 0)?  ([arrayimage count]-1):
    swipee%[arrayimage count];


    Allimages.image=[UIImage imageNamed:  [arrayimageobjectAtIndex:indexPath]];
    }}

http://i.stack.imgur.com/apDEw.png

http://i.stack.imgur.com/EiURG.png

http://i.stack.imgur.com/1q799.png

2 个答案:

答案 0 :(得分:1)

添加此

 [imageview setUserInteractionEnabled=YES];

为swipegesture识别器类创建对象

UISwipeGestureRecognizer *leftSwipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(onSwipe)];

[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];

将swipeGesture添加到imageView

[imageView addGestureRecognizer:leftSwipe];

-(void)onSwipe:(UISwipeGestureRecognizer)swipe{
 if(swipe.direction == UISwipeGestureRecognizerDirectionLeft)
  {
   NSLog(@"leftSwipe");
  }
}

如果您想了解更多信息:请检查  Gesture recognizer (swipe) on UIImageView

答案 1 :(得分:0)

在您的详细信息视图中,将UserInteractionEnabledt设置为您的图像视图的“是”。

 [imageView setUserInteractionEnabled:YES];

添加滑动手势事件

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

// Adding the swipe gesture on image view
[imageView addGestureRecognizer:swipeLeft];
[imageView addGestureRecognizer:swipeRight];

使用以下代码处理滑动手势事件。

- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Left Swipe");
    }

    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"Right Swipe");   
    } 

}

否则您可以使用第三方工具

https://www.cocoacontrols.com/controls/pagedflowview