所以我想创建一个视图,你可以从左到右滑动图像。我已经能够弹出一个空白页面显示我的标题并识别滑动手势(左或右)并根据需要对indexToShow进行icnreasing。但是,我无法看到任何图像只是一个空白屏幕。任何帮助将不胜感激。
这就是我所做的:
<。>文件中的
UIImageView *myImageView;
int indexToShow;
<。>文件中的
@interface myController ()
@property (nonatomic, strong) NSMutableArray *imgArray;
@end
@implementation myController
@synthesize imgArray;
然后我
- (void)viewDidLoad
{
[super viewDidLoad];
imgArray = [[NSMutableArray alloc] initWithObjects:@"image1.png",@"image2.png",@"image3.png",@"image4.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];
myImageView.image = [imgArray objectAtIndex:indexToShow];
}
- (void)swipeRight:(UISwipeGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
if ((indexToShow-1) < -1) {
indexToShow = imgArray.count-1;
}
myImageView.image = [imgArray objectAtIndex:indexToShow];
indexToShow--;
}
}
- (void)swipeLeft:(UISwipeGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
if ((indexToShow+1) > imgArray.count ) {
indexToShow = 0;
}
myImageView.image = [imgArray objectAtIndex:indexToShow];
indexToShow++;
}
}
然后在我看来,我有
[super viewWillAppear:animated];
self.title = @"Hi";
[self.view addSubview:tutorialImageView];
答案 0 :(得分:0)
我建议您使用UICollectionView而不是为了提高效率而执行此操作,但您当前的问题有一个简单的修复。创建数组时,您将其设为字符串数组(文件名)。
imgArray = [[NSMutableArray alloc] initWithObjects:@"image1.png",@"image2.png",@"image3.png",@"image4.png", nil];
但是当您访问它时,您将获取此字符串(文件名)并将其设置为图像视图的图像。
myImageView.image = [imgArray objectAtIndex:indexToShow];
相反,你应该像@Larme建议的那样做,并使用以下
myImageView.image = [UIImage imageName:[imgArray objectAtIndex:indexToShow]];
这将从数组中选定索引处的文件名创建UIImage,并将其设置为图像视图的图像。