简体   繁体   English

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

[英]How to create swipe gesture for right/left in detailed view UIimage

I am working on a food app in which each tableViewCell shows different recipes names. 我正在开发一个食品应用程序,其中每个tableViewCell显示不同的配方名称。 When a user taps on a recipe (in a cell), a detailed view will be opened. 当用户点击配方时(在单元格中),将打开详细视图。

I am now trying to implement a swipe gesture for UIImageView which shows different images on swipe. 我现在正在尝试为UIImageView实现滑动手势,该手势在滑动时显示不同的图像。 How do I implement that? 我该如何实施?

 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/apDEw.png

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

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

Add this 加上这个

 [imageview setUserInteractionEnabled=YES];

create object for swipegesture recognizer class 为swipegesture识别器类创建对象

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

[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];

Add swipeGesture to imageView 将swipeGesture添加到imageView

[imageView addGestureRecognizer:leftSwipe];

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

if you want more information: just check Gesture recognizer (swipe) on UIImageView 如果您需要更多信息:只需在UIImageView上检查“ 手势识别器”(滑动)即可

In your detail View Make UserInteractionEnabledt to YES of your imageview. 在您的详细信息视图中,将UserInteractionEnabledt设置为imageview的YES。

 [imageView setUserInteractionEnabled:YES];

Adding a Swipe Gesture Events 添加滑动手势事件

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];

And Handle Swipe Gesture Events using below code. 并使用以下代码处理滑动手势事件。

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

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

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

}

Or else You can use Third Party Tool 否则,您可以使用第三方工具

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM