可滑动的图像视图

Swipable Image View(可滑动的图像视图)
本文介绍了可滑动的图像视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试创建一个视图,以便您可以从左向右滑动图像。我已经让它能够弹出一个空白页面,显示我的标题,识别滑动手势(左或右),并根据需要创建indexToShow。然而,我看不到我的任何图像,只看到一个空白屏幕。如有任何帮助,我们将不胜感激。

以下是我所做的:

在.h文件中

UIImageView *myImageView;
int indexToShow;

在.m文件中

@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];

推荐答案

为了提高效率,我建议您使用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,并将其设置为图像视图的图像。

这篇关于可滑动的图像视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)