我有五张图片,它们被命名为1.jpg 2.jpg 3.jpg 4.jpg 5.jpg,当我的手指在ipad的屏幕上移动时(从右到左),UIImageView将显示来自的图像1.jpg到5.jpg,再从1.jpg到5.jpg,循环,直到我的手指触摸结束,如果我的手指从左向右移动,UIImageView将显示从5.jpg到1.jpg的图像,循环也。 我使用以下代码
int currentTag = 1;
NSArray* pages;
-(void)viewDidLoad{
[super viewDidLoad];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
pages=[[NSArray alloc] initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",nil];
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft)
{
if (currentTag<[pages count]) {
[imgview setImage:[UIImage imageNamed:[pages objectAtIndex:currentTag]]];
[imgview setUserInteractionEnabled:YES];
imgview.tag=currentTag;
currentTag++;
}
}
}
但它不起作用。那么正确的做法是什么?谢谢
答案 0 :(得分:0)
似乎您正在将uemageRecognizer添加到UIImageView的superView中,但您也正在使用它[imgview setUserInteractionEnabled:YES] ;.评论此行和您的代码应该有效。因为当你设置[imgview setUserInteractionEnabled:YES];然后所有的触摸都将由UIImageView处理,我假设你的ImageView的大小等于视图的大小。