我是iPhone的新手,我正在使用单点按钮在KASlideShow自定义控件上使用UITapGestureRecognizer。我已经使用XIB添加KASlideShow以在几个间隔之后滑动三个图像。我的问题是,点击图像后我没有得到正确的索引。你可以帮我解决下面代码中我的错误:
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);
}
每次我得到&#34;幻灯片放映标签是&#34; 2,我想以全屏显示特定的所选图像。请建议我达到正确的标签。
谢谢!
答案 0 :(得分:0)
您获得了正确的标签。
标记应该是2,因为你的arraySlideShowImages
计数是3,你在for循环中写了slideshow.tag = i;
。标签将被修改三次为0,1,2。
如果您希望根据图像将其更改为0,1和2,则必须将标记设置为addImage
方法内的图像视图。还要确保使用1而不是0的tag属性。所以将你的标签设置为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);
}
答案 1 :(得分:0)
一个。设置标记
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
}
1.在KASlideShow.h类中添加一个方法
@protocol KASlideShowDelegate <NSObject>
- (void)kaSlideShowSingleTap:(KASlideShow *) slideShow;
并在KASlideShow.m类的方法handleSingleTap中添加以下代码
if([self.delegaterespondsToSelector:@selector(kaSlideShowSingleTap:)])
[self.delegate kaSlideShowSingleTap:self];
并在View控制器中调用Delegate方法(您想要获取标签的位置)
- (void)kaSlideShowSingleTap:(KASlideShow *) slideShow
{
NSLog(@"kaSlideShowSingleTap, index : %@",@(slideShow.currentIndex)); }