Swipable Image View

旧时模样 提交于 2020-01-25 03:32:06

问题


so I am trying to create a view were you can swipe through images left to right. I have gotten it able to pop up a blank page showing my title and recognizing the swipe gestures (left or right) and icnreasing the indexToShow as required. However, I am not able to see any of my images just a blank screen. Any help would be appreciated.

Here is what I have done:

in .h file

UIImageView *myImageView;
int indexToShow;

in .m file

@interface myController ()
@property (nonatomic, strong) NSMutableArray *imgArray;
@end
@implementation myController
@synthesize imgArray;

and then I have

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

and then in my viewwillappear I have

[super viewWillAppear:animated];
self.title = @"Hi";
[self.view addSubview:tutorialImageView];

回答1:


I recommend that you use UICollectionView instead of doing this for efficiency purposes, but your current problem has a simple fix. When you created your array, you made it an array of strings (file names).

imgArray = [[NSMutableArray alloc] initWithObjects:@"image1.png",@"image2.png",@"image3.png",@"image4.png", nil];

But when you access it, you're taking this string (file name) and setting it as the image view's image.

myImageView.image = [imgArray objectAtIndex:indexToShow];

Instead, you should do as @Larme suggested and use the following

myImageView.image = [UIImage imageName:[imgArray objectAtIndex:indexToShow]];

This will create a UIImage from the file name at the selected index in the array, and set it as the image view's image.



来源:https://stackoverflow.com/questions/20150636/swipable-image-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!