这可能是一个简单的问题,但我已经搜索了几个小时试图找出它。
最初我试图创建一个允许我通过左右滑动来更改背景图像的视图。在无法找到任何简单的方法之后,我想我可以用一组图像制作一个数组,并将背景图像设置为该数组中的特定对象。我已经成功地设置了它,但是我无法弄清楚如何使用手势来改变索引处对象的值。我正在想象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];
答案 0 :(得分:1)
您不必添加5个UIImageViews,只需添加一个并更改其图像:
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];
}
答案 1 :(得分:0)
使用您自己的代码:
in .m
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIImageView *mainViewImage;
@property (nonatomic) int currentValue;
@property (nonatomic, strong) NSArray *vacationImages;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// change the size of the frame of the image here
self.mainViewImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 40, 320, 200)];
[self.view addSubview:self.mainViewImage];
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"];
self.vacationImages = [NSArray arrayWithObjects: airplaneImage, beachImage, cityImage, lodgeImage, farmImage, nil];
self.currentValue = 0;
UIImage *currentImage = [self.vacationImages objectAtIndex:self.currentValue];
self.mainViewImage.image = currentImage;
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];
}
- (void) handleSwipeFromLeft: (id) sender
{
if (self.currentValue == 0)
{
self.currentValue = 4;
}
else
{
self.currentValue--;
}
UIImage *currentImage = [self.vacationImages objectAtIndex:self.currentValue];
self.mainViewImage.image = currentImage;
}
- (void) handleSwipeFromRight: (id) sender
{
if (self.currentValue == 4)
{
self.currentValue = 0;
}
else
{
self.currentValue++;
}
UIImage *currentImage = [self.vacationImages objectAtIndex:self.currentValue];
self.mainViewImage.image = currentImage;
}
@end