在iOS中使用UITapgestureRecograiser获取不正确的标记
我对iPhone非常陌生,我正在一个KASlideShow自定义控件上使用UITapGestureRecognitizer,只需点击一次。我添加了KASlideShow,使用XIB在几个间隔后滑动三张图像。我的问题是,我在点击图像后没有得到正确的索引。你能帮我一下我在下面代码中的错误在哪里吗在iOS中使用UITapgestureRecograiser获取不正确的标记,ios,ios7,uitapgesturerecognizer,Ios,Ios7,Uitapgesturerecognizer,我对iPhone非常陌生,我正在一个KASlideShow自定义控件上使用UITapGestureRecognitizer,只需点击一次。我添加了KASlideShow,使用XIB在几个间隔后滑动三张图像。我的问题是,我在点击图像后没有得到正确的索引。你能帮我一下我在下面代码中的错误在哪里吗 UITapGestureRecognizer *tapGestureRecognizerInstruction = [[UITapGestureRecognizer alloc]initWithTarget
UITapGestureRecognizer *tapGestureRecognizerInstruction = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapOnView:)];
[tapGestureRecognizerInstruction setNumberOfTapsRequired:1];
tapGestureRecognizerInstruction.delegate = self;
slideshow.delegate = self;
[slideshow setDelay:1]; // Delay between transitions
[slideshow setTransitionDuration:.5]; // Transition duration
[slideshow setTransitionType:KASlideShowTransitionSlide];
[slideshow setImagesContentMode:UIViewContentModeScaleAspectFill];
[slideshow addGestureRecognizer:tapGestureRecognizerInstruction];
arraySlideShowImages = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"1.jpg"], [UIImage imageNamed:@"2.jpg"], [UIImage imageNamed:@"3.jpg"],nil]; //array of images
for (int i=0; i<[arraySlideShowImages count]; i++) {
slideshow.tag = i;
[slideshow addImage:[arraySlideShowImages objectAtIndex:i]];
}
[slideshow start];
-(void)handleTapOnView:(id)sender
{
UIGestureRecognizer *recognizer = (UIGestureRecognizer*)sender;
KASlideShow * slideshow1 = (KASlideShow *)recognizer.view;
NSLog(@"slide show tag is = %ld", (long)slideshow1.tag);
}
UITapGestureRecognizer*tapgesturerecognizer指令=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapOnView:)];
[TapGestureRecognitizer指令集所需TapNumber:1];
tappesturerecognizerInstruction.delegate=self;
slideshow.delegate=self;
[幻灯片放映设置延迟:1];//过渡之间的延迟
[幻灯片放映设置转换持续时间:.5];//过渡期
[幻灯片放映setTransitionType:KASlideShowTransitionSlide];
[幻灯片放映设置图像内容模式:UIViewContentModeScaleAspectFill];
[幻灯片放映添加GestureRecognitzer:TapGestureRecognitzerInstruction];
arraySlideShowImages=[[NSMutableArray alloc]initWithObjects:[UIImage ImageName:@“1.jpg”],[UIImage ImageName:@“2.jpg”],[UIImage ImageName:@“3.jpg”],nil]//图像数组
对于(inti=0;i,您得到了正确的标记
标记应始终为2,因为您的arraySlideShowImages
计数为3,并且您已在for循环内编写了slideshow.tag=i;
。标记将被修改为0,1,2三次
如果要根据图像将其更改为0、1和2,则必须将标记设置为addImage
方法内的图像视图。还要确保使用标记属性1而不是0。因此,将标记设置为i+1
for (int i=0; i<[arraySlideShowImages count]; i++)
{
[slideshow addImage:[arraySlideShowImages objectAtIndex:i] Index:i+1];
}
//modify your addImage method
- (void)addImage:(UIImage *)image Index:(int)itemIndex
{
//your image view tag as itemIndex
}
-(void)handleTapOnView:(id)sender
{
UITapGestureRecognizer *recognizer = (UITapGestureRecognizer *)sender;
UIImageView* clickedImageView = [recognizer.view isKindOfClass:[UIImageView class]]?(UIImageView *)recognizer.view:nil;
NSLog(@"clickedImageView tag is = %d", clickedImageView.tag);
}
for(int i=0;iA.设置标记
for(int i=0; i<[arraySlideShowImages count]; i++)
{
[slideshow addImage:[arraySlideShowImages objectAtIndex:i] Index:i+1];
}
//modify your addImage method
- (void)addImage:(UIImage *)image Index:(int)itemIndex
{
//your image view tag as itemIndex
}
并在视图控制器中调用下面的委托方法(从中获取标记)
日志<代码>[幻灯片显示标签]
在您的for loop
之后,您可能会明白问题所在。您可能希望获得当前索引
。是的@Larme,for loop的bcos我总是得到最后一个数组计数,即2。但是如何获得当前索引呢,先生?您需要为每个幻灯片创建点击手势。这是KaSlideShow的一个属性,似乎来自GitHub。当您有对视图本身的引用时,为什么还要使用标记?@RamshadThank,我希望标记应根据图像更改为0、1和2。@user2786:我已修改了答案。请告诉我状态。请使用它来实现正确的幻灯片标记。
@protocol KASlideShowDelegate <NSObject>
- (void)kaSlideShowSingleTap:(KASlideShow *) slideShow;
if([self.delegaterespondsToSelector:@selector(kaSlideShowSingleTap:)])
[self.delegate kaSlideShowSingleTap:self];
- (void)kaSlideShowSingleTap:(KASlideShow *) slideShow
{
NSLog(@"kaSlideShowSingleTap, index : %@",@(slideShow.currentIndex)); }