gpt4 book ai didi

ios - 制作具有滑动到下一张图像功能的 ImageView

转载 作者:行者123 更新时间:2023-11-29 12:40:39 25 4
gpt4 key购买 nike

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

enter image description here enter image description here

我觉得跟stack overflow这个问题差不多 here

我理解手势部分并且它有效,但它不像我想要的那样具有动画效果。我希望它能像您在照片应用程序中浏览照片时那样工作。有帮助吗?

这是我的代码,但我想我只需要使 ImageView 成为 subview

#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++;
}
}

@结束

最佳答案

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

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

创建一个包含 UImageNSArray。创建一个 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

关于ios - 制作具有滑动到下一张图像功能的 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25050470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com