gpt4 book ai didi

ios - 如何使用按钮/手势来更改值? - iOS

转载 作者:行者123 更新时间:2023-11-29 12:50:37 24 4
gpt4 key购买 nike

这可能是一个简单的问题,但我已经搜索了几个小时试图弄明白。

最初我试图创建一个允许我通过左右滑动来更改背景图像的 View 。在找不到任何简单的方法来做到这一点之后,我想我可以用一组图像制作一个数组,并将背景图像设置为该数组中的特定对象。我已经成功设置了它,但我无法弄清楚如何使用手势来更改索引处对象的值。我正在想象一些类似 currentImage++currentImage -- 的东西来改变值,但我不知道如何实现它。

如果有人知道如何做到这一点,请提供帮助。或者,如果有人有更有效的方法来做到这一点,我也想听听。我是 iOS 编程的新手。

这就是 viewDidLoad 中的内容。

UIImage *beachImage = [UIImage imageNamed:@"Malibu_Sunset_1.jpg"];
UIImage *cityImage = [UIImage imageNamed:@"chicago_marina_city_parking_garages.jpg"];
UIImage *lodgeImage = [UIImage imageNamed:@"ski_lodge.jpg"];
UIImage *airplaneImage = [UIImage imageNamed:@"airplanes-work-1.jpg"];
UIImage *farmImage = [UIImage imageNamed:@"ideal-farm-house-photo.jpg"];

NSMutableArray *vacationImages = [NSMutableArray arrayWithObjects: airplaneImage, beachImage, cityImage, lodgeImage, farmImage, nil];

int numberOfImages = [vacationImages count];
int currentValue = 0;
UIImage *currentImage = [vacationImages objectAtIndex:currentValue];

self.mainViewImage.image = currentImage;



for (UIImage *currentImage in vacationImages) {
UIImageView *line = [[UIImageView alloc] initWithImage:currentImage];
line.hidden = YES;
line.tag = currentValue;
[self.view addSubview:line];
numberOfImages++;
}

UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromLeft)];
[leftRecognizer setDirection: UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:leftRecognizer];


UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromRight)];
[rightRecognizer setDirection: UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:rightRecognizer];

最佳答案

您不必添加 5 个 UIImageView,只需添加一个并更改其图像即可:

in .h:
UIImageView *bgImageView;
NSArray *vacationImages;
int currentImageIndex;

in .m
-(void) handleSwipeFromLeft
{
currentImageIndex--;
if(currentImageIndex< 0)
currentImageIndex= vacationImages.count;

NSString *imageName = [vacationImages objectAtIndex:currentImageIndex];
bgImageView.image = [UIImage imageNamed:imageName];
}

-(void) handleSwipeFromRight
{
currentImageIndex++;
if(currentImageIndex>= vacationImages.count)
currentImageIndex= 0;

NSString *imageName = [vacationImages objectAtIndex:currentImageIndex];
bgImageView.image = [UIImage imageNamed:imageName];
}

关于ios - 如何使用按钮/手势来更改值? - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22517166/

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