【问题标题】:How to show NextImage in ImageView when User SwipeLeft in ImageView through UISwipeGestureRecognizer in iOS?当用户通过iOS中的UISwipeGestureRecognizer在ImageView中SwipeLeft时如何在ImageView中显示NextImage?
【发布时间】:2014-11-24 06:27:29
【问题描述】:

我是 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 添加手势的代码,例如

 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 图像返回索引图像。请给我解决方案。

【问题讨论】:

  • 显示您的 UISwipeGestureRecognizer 操作
  • @Sport UISwipeGestureRecognizer action -(void)slideToLeftWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer { for(index=0;index

标签: ios uiscrollview uiimageview uiswipegesturerecognizer


【解决方案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++;
   }
 }

将索引设为全局变量,在 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"]];


    }
 }

【讨论】:

  • 但是兄弟,这里我的数组是包含图像 URL 链接。所以我写成 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"]];索引++;但它在 NSDictionary *dict=[self.imagesa objectAtIndex:index]; 行中显示错误
  • 兄弟,我已经修改了我的答案,再看看,如果这对你有帮助。
  • 这里我的 imageview 在我的 Scrollview 中,所以我在 Imageview 中添加了一个 SWipeGesture,但是你的代码不起作用,我可以在我的 Scrollview 中添加一个 SwipeGesture 吗?
  • 能否贴出代码,在scrollview上添加手势
  • 你滚动视图一次只有一张图片还是多张图片?
猜你喜欢
  • 1970-01-01
  • 2014-07-18
  • 2014-07-22
  • 2019-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-16
  • 1970-01-01
相关资源
最近更新 更多