Uiview 检测到滑动手势,但未显示图像(UIWipegestureRecognitor、UIImageView和NSArray)

Uiview 检测到滑动手势,但未显示图像(UIWipegestureRecognitor、UIImageView和NSArray),uiview,uiimageview,nsarray,swipe,uiswipegesturerecognizer,Uiview,Uiimageview,Nsarray,Swipe,Uiswipegesturerecognizer,我正在尝试在两个图像之间滑动。可以识别该手势,但不显示任何图像 我正试图以编程的方式来做这件事。首先,我创建了一个在.h文件中声明的UIImageView。该图像视图称为imageView 在.m文件中,我实现了该视图。我还添加了左右滑动手势识别器。滑动触发一个称为HandleSweep的动作。我知道刷卡是可以识别的,因为刷卡发生时,输出日志中会显示NSLog语句 HandleSweep操作从NSArray获取图像,并根据向左还是向右滑动显示图像。该方法将图像置于imageView中 我的问题是

我正在尝试在两个图像之间滑动。可以识别该手势,但不显示任何图像

我正试图以编程的方式来做这件事。首先,我创建了一个在.h文件中声明的UIImageView。该图像视图称为imageView

在.m文件中,我实现了该视图。我还添加了左右滑动手势识别器。滑动触发一个称为HandleSweep的动作。我知道刷卡是可以识别的,因为刷卡发生时,输出日志中会显示NSLog语句

HandleSweep操作从NSArray获取图像,并根据向左还是向右滑动显示图像。该方法将图像置于imageView中

我的问题是HandleSweep不显示任何图像

我的代码如下:

@implementation firstStartViewController
@synthesize imageView;
int imageIndex = 1;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];


imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));

imageView.center = CGPointMake(CGRectGetWidth(self.view.bounds)/2.0f, CGRectGetHeight(self.view.bounds)/2.0f + 75);

imageView.userInteractionEnabled = YES;

[self.view addSubview:imageView];



UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeleft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];



UISwipeGestureRecognizer *swiperight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swiperight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];


UIBarButtonItem *dismiss_btn = [[UIBarButtonItem alloc] initWithTitle:@"Start App" style:UIBarButtonItemStylePlain target:self action:@selector(dismissModal:)];

self.navigationItem.rightBarButtonItems = [NSMutableArray arrayWithObjects:dismiss_btn, nil];
}


- (IBAction)handleSwipe:(UIGestureRecognizer *)sender {

NSLog( @"Swiped");


NSArray *images=[[NSArray alloc] initWithObjects:
                 @"image1.png",
                 @"image2.png", nil];

UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];

switch (direction) {
    case UISwipeGestureRecognizerDirectionLeft:
        imageIndex++;
        break;
    case UISwipeGestureRecognizerDirectionRight:
        imageIndex--;
        break;
    default:
        break;
}
imageIndex = (imageIndex < 0) ? ([images count] -1): imageIndex % [images count];
imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
}
@implementation firstStartViewController
@综合图像视图;
int-imageIndex=1;
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后执行任何其他设置。
self.view.backgroundColor=[UIColor whiteColor];
imageView.frame=CGRectMake(0,0,CGRectGetWidth(self.view.bounds),CGRectGetHeight(self.view.bounds));
imageView.center=CGPointMake(CGRectGetWidth(self.view.bounds)/2.0f,CGRectGetHeight(self.view.bounds)/2.0f+75);
imageView.userInteractionEnabled=是;
[self.view addSubview:imageView];
UISwipeLeft=[[UISwipeGestureRecognitzer alloc]initWithTarget:self action:@selector(handleSwipe:)];
swipelft.direction=uiswipgesturerecognizerDirectionLeft;
[self.view addgesturecognizer:swipleft];
UISweepGestureRecognizer*swiperight=[[UISweepGestureRecognizer alloc]initWithTarget:self action:@selector(HandleSweep:)];
swiperight.direction=UISWipGestureRecognitizerDirectionRight;
[self.view addgesturecognizer:swiperight];
UIBarButtonItem*Discouse_btn=[[UIBarButtonItem alloc]initWithTitle:@“启动应用程序”样式:UIBarButtonItemStylePlain目标:自我操作:@selector(dismissModal:)];
self.navigationItem.RightBarButtonims=[NSMutableArray arrayWithObjects:Discouse_btn,nil];
}
-(iAction)无手柄擦除:(UIgestureRecognitor*)发送器{
NSLog(@“刷卡”);
NSArray*图像=[[NSArray alloc]initWithObjects:
@“image1.png”,
@“image2.png”,无);
UISweepGestureRecognitzerDirection=[(UISweepGestureRecognitzer*)发送方方向];
开关(方向){
案例UISweepGestureRecognitizerDirectionLeft:
imageIndex++;
打破
案例UISweepGestureRecognitizerDirectionRight:
图像索引--;
打破
违约:
打破
}
imageIndex=(imageIndex<0)?([图像计数]-1):imageIndex%[图像计数];
imageView.image=[UIImage ImageName:[images objectAtIndex:imageIndex]];
}

您应该在viewDidLoad方法中将图像索引设置为默认值(可能为0)

只需在viewDidLoad中添加这一行

imageIndex = 0;

@雪莉:我编辑了我的代码,告诉你我在哪里初始化imageIndex。它位于at实现部分下,而不是viewDidLoad中。让我试试你的建议。@Shiri:我发现了问题。这是因为NSArray正在返回(null)值。现在我需要找出如何修复它。我在这里打开了一个新主题