【问题标题】:Making an image view with the ability to swipe to a next image制作一个能够滑动到下一张图像的图像视图
【发布时间】:2014-07-31 03:40:38
【问题描述】:

我无法显示图片,因为我没有足够的声誉,但我试图在视图中有一个图像视图,其中包含一系列图片,您可以滑动以从一个图像过渡到下一个图像。但唯一改变的是图像视图中的图像,而不是整个视图。

我认为它类似于堆栈溢出的这个问题 here

我理解手势部分,它可以工作,但它不像我想要的那样动画。我希望它像您在照片应用程序上滑动照片时一样工作。有什么帮助吗?

这是我的代码,但我想我只需要将图像视图设为子视图

#import "PizzaViewController.h"

@interface PizzaViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
@property (nonatomic, strong) NSMutableArray *imgArray;
@end

@implementation PizzaViewController
@synthesize imgArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
    imgArray = [[NSMutableArray alloc] initWithObjects:@"recipe instructions 1-01.png",@"recipe instructions 2-01.png",@"recipe instructions 3-01.png", nil];
    indexToShow = 0;
    UISwipeGestureRecognizer *gestureRight;
    UISwipeGestureRecognizer *gestureLeft;
    gestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self      action:@selector(swipeRight:)];
    gestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
    [gestureLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [[self view] addGestureRecognizer:gestureRight];
    [[self view] addGestureRecognizer:gestureLeft];
    self.myImageView.image = [UIImage imageNamed:[imgArray objectAtIndex:indexToShow]];
}

- (void)swipeRight:(UISwipeGestureRecognizer *)gesture
{
    if ((gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) {

        if ((indexToShow-1) < -1) {
            indexToShow = imgArray.count-1;
        }
        self.myImageView.image = [UIImage imageNamed:[imgArray objectAtIndex:indexToShow]];
        indexToShow--;
    }
}

- (void)swipeLeft:(UISwipeGestureRecognizer *)gesture
{
    if ((gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) {

        if ((indexToShow+1) > imgArray.count ) {
            indexToShow = 0;
        }
        self.myImageView.image = [UIImage imageNamed:[imgArray objectAtIndex:indexToShow]];
        indexToShow++;
    }
}

@结束

【问题讨论】:

  • 你必须使用scrollView并将你所有的imageview放在scrollView里面。
  • 您可以在视图或图像视图上使用 SwipeGestureRecognizer。我在我的一个项目中做了同样的事情,而且效果很好。
  • 我的代码对你有用吗?
  • 没有。抱歉,我问的不是这个。
  • “我理解手势部分,它可以工作,但它的动画效果不像我想要的那样。我希望它像你在照片应用上滑动照片时一样工作”。我做了更多的研究,我想我可能不得不使用 panGesture 或滚动视图

标签: ios uiimageview uigesturerecognizer subview


【解决方案1】:

要让它像照片一样移动,您需要将 UImageView 放入 UIScrollView

如果您不希望它像照片一样移动,请执行以下操作:

创建一个包含UImages 的NSArray。创建一个 int,它将作为要显示的数组中图像的索引。让手势识别器更改一个 int 值(向右滑动时增加一,向左滑动时减少一)。每次滑动后,将UImageView 中的图像更改为 int 值索引处的对象。

代码示例:

设置数组

- (void) setImages
{
        UIImage *image1 = [UIImage imageNamed:@"1.png"];
        UIImage *image2 = [UIImage imageNamed:@"2.png"];
        UIImage *image3 = [UIImage imageNamed:@"3.png"];
        UIImage *image4 = [UIImage imageNamed:@"4.png"];
        UIImage *image5 = [UIImage imageNamed:@"5.png"];
        UIImage *image6 = [UIImage imageNamed:@"6.png"];
        UIImage *image7 = [UIImage imageNamed:@"7.png"];
        UIImage *image8 = [UIImage imageNamed:@"8.png"];

        NSArray *pictuesArray = [[NSArray alloc] initWithObjects:image1, image2, image3, image4, image5, image6, image7, image8, nil];
self.pictures = pictuesArray;

}

返回和下一步按钮

- (IBAction)backButton:(UIButton *)sender
{
    if (self.arrayPosition > 0) {
        self.arrayPosition--;
        [self.image setImage:[self.pictures objectAtIndex:self.arrayPosition]];
    }

}

- (IBAction)nextButton:(UIButton *)sender
{
    if (self.arrayPosition < self.pictures.count - 1){
        self.arrayPosition++;
        [self.image setImage:[self.pictures objectAtIndex:self.arrayPosition]];
    }
}

self.pictures //is an array of the images
self.arrayPosition //is the index
self.image //is the UImageView

【讨论】:

  • 这是一个答案吗?用代码示例详细说明。
  • 我认为UIScrollView 是一种更原生、更好的解决方案。另外,它会更好地处理动画。
  • 不,我完全理解,但感谢怀亚特。我现在要编辑
  • 我在代码中添加了一个示例。这是一种非常简单且基本的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 2023-04-03
  • 2014-10-16
  • 2011-05-21
  • 2012-02-15
相关资源
最近更新 更多