如何通过iOS中的UISwipeGestureRecognizer在ImageView中使用用户SwipeLeft时在ImageView中显示NextImage?

时间:2014-11-24 06:27:29

标签: ios uiscrollview uiimageview uiswipegesturerecognizer

我是iOS开发中的新手。我想在我的应用程序中创建一个应用程序,我制作一个UIScrollview,在这个UIScrollView中我添加一个UIImageview。当viewDidAppear我将imageview图像设置为

-(void)viewDidAppear:(BOOL)animated
{
NSDictionary *dict=[self.imagesa objectAtIndex:0];
NSString *imagelink=[dict valueForKey:@"link"];
NSLog(@"imageLink %@",imagelink);
[self.bigImage sd_setImageWithURL:[NSURL URLWithString:imagelink] placeholderImage:[UIImage imageNamed:@"1.png"]];
}

为我的Imageview和Scrollvie添加Gesture的代码,如

 UISwipeGestureRecognizer *swipeGestureLeftdirection=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(slideToLeftWithGestureRecognizer:)];
swipeGestureLeftdirection.delegate=self;
swipeGestureLeftdirection.direction=UISwipeGestureRecognizerDirectionLeft;
[self.bigImage addGestureRecognizer:swipeGestureLeftdirection];
[self.bigScrollview addGestureRecognizer:swipeGestureLeftdirection];

 UISwipeGestureRecognizer *swipeGestureRightdirection=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(slideToRighttWithGestureRecognizer:)];
swipeGestureRightdirection.delegate=self;
swipeGestureRightdirection.direction=UISwipeGestureRecognizerDirectionRight;
[self.bigImage addGestureRecognizer:swipeGestureRightdirection];
[self.bigScrollview addGestureRecognizer:swipeGestureRightdirection];

这里Self.bigimage是我的UIImageview,self.bigScrollview是我的UIScrollView。 现在我想当用户滑动离开然后我想要UIImageview图像作为self.imagesa下一个索引,当向右滑动然后我想要UIImageVIew图像返回索引图像如何可能。请给我解决方案。

1 个答案:

答案 0 :(得分:1)

不要在里面使用循环,slideToLeftWithGestureRecognizer:

像这样编写代码

 -(void)slideToLeftWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer 
{ 
     if(index<[self.imagesa count])
     {
         NSDictionary *dict=[self.imagesa objectAtIndex:index];
        NSString *imagelink=[dict valueForKey:@"link"];
        NSLog(@"imageLink %@",imagelink);
       [self.bigImage sd_setImageWithURL:[NSURL URLWithString:imagelink] placeholderImage:        [UIImage imageNamed:@"1.png"]];

    index++;
   }
 }

将index作为全局可变,在viewDidLoad上使用0进行初始化。

  -(void)slideToRighttWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer 
   { 
      if(index>0)
     {
        index--;
        NSDictionary *dict=[self.imagesa objectAtIndex:index];
        NSString *imagelink=[dict valueForKey:@"link"];
        NSLog(@"imageLink %@",imagelink);
       [self.bigImage sd_setImageWithURL:[NSURL URLWithString:imagelink] placeholderImage:        [UIImage imageNamed:@"1.png"]];


    }
 }