【问题标题】:How to change image from UIImageView by sliding right or left from UIViewController如何通过从 UIViewController 向右或向左滑动来更改 UIImageView 中的图像
【发布时间】:2016-02-18 03:45:21
【问题描述】:

我有一个UIViewController,其中有一个UIImageView,还有一个UIImage 数组。如何开发向右或向左滑动功能以将图像更改为下一个数组位置?

【问题讨论】:

    标签: iphone ios uiviewcontroller uiimageview


    【解决方案1】:

    常见的方法是 UIScrollView 的大小与其中一张图像相同,并启用分页。

    将图像添加为子视图并像这样设置内容大小...

    NSArray *images;
    CGSize imageSize;
    
    self.scrollView.frame = CGRectMake(10,10,imageSize.width,imageSize.height);
    self.scrollView.contentSize = CGSizeMake(imageSize.width * images.count, imageSize.height);
    self.scrollView.pagingEnabled = YES;
    
    CGFloat xPos = 0.0;
    
    for (UIImage *image in images) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = CGRectMake(xPos, 0.0, imageSize.width, imageSize.width);
        [self.scrollView addSubview:imageView];
        xPos += imageSize.width;
        // assuming ARC, otherwise release imageView
    }
    

    您还可以关闭反弹、滚动指示器等,具体取决于您想要的效果的详细信息。

    【讨论】:

    • 我试过了,不确定我是否正确地遵循了你的建议,它不起作用,滚动视图保持空白。也许我错过了一些东西,因为我不了解对象、事件是如何工作的......谢谢
    • 这个答案把我带到了解决我问题的链接:iosdevnotes.com/2011/03/uiscrollview-paging。谢谢 Danh,我投了赞成票并标记为已回答
    【解决方案2】:

    通过使用 UIScrollView 和 UIPageControl,拖放 UIScrollView 和 UIPageControl 确保它们不重叠,创建两者的 IBoutLet.Set

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // 1
    imageArray = [NSArray arrayWithObjects:
                         [UIImage imageNamed:@"image2.jpg"],
                         [UIImage imageNamed:@"man-actor-Ashton-Kutcher-Wallpaper.jpg"],
                         [UIImage imageNamed:@"image1.jpg"],
                         [UIImage imageNamed:@"man-curl.jpg"],
                         [UIImage imageNamed:@"man-in-suit.jpg"],
                         nil];
    
    
      for (int i = 0; i < imageArray.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
    
        UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
        subview.image = [imageArray objectAtIndex:i];
        subview.contentMode = UIViewContentModeScaleAspectFit;
        [self.scrollView addSubview:subview];
      }
    
      self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageArray.count, self.scrollView.frame.size.height);
      pageControl.numberOfPages = imageArray.count;
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)sender {
        // Update the page when more than 50% of the previous/next page is visible
      CGFloat pageWidth = self.scrollView.frame.size.width;
      int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
      self.pageControl.currentPage = page;
    }
    

    【讨论】:

      【解决方案3】:

      您可以像这样使用UISwipeGestureRecognizer 简单地获得向左或向右滑动方向

      swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe:)];
      [swipeGesture setNumberOfTouchesRequired:1];
      [swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
      [appView addGestureRecognizer:swipeGesture];
      

      方法detectSwipe 声明用于处理您的前进和后退以使用 UIImage 数组。也可以查看Apple's SimpleGestureRecognizers Demo

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 2013-02-17
        • 2014-07-07
        • 1970-01-01
        • 1970-01-01
        • 2011-03-29
        • 2018-02-05
        • 1970-01-01
        • 2017-01-05
        • 2013-02-14
        相关资源
        最近更新 更多