关于触摸移动功能
我有五张图片,它们被命名为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++;
}
}
}
,但它无法工作。那么正确的做法是什么?谢谢
i have five images,they are named 1.jpg 2.jpg 3.jpg 4.jpg 5.jpg,when my finger moves on the screen of ipad(from right to left),the UIImageView will show the images from 1.jpg to 5.jpg,and from 1.jpg to 5.jpg again, loop,until my finger touchEnd,if my finger moves from left to right,the UIImageView will show the images from 5.jpg to 1.jpg,loop also.
i use the following code
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++;
}
}
}
but it can not work. so what is the right way to do?thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您正在将 guestureRecognizer 添加到 UIImageView 的 superView 但您也在使用它 [imgview setUserInteractionEnabled:YES]; 。注释此行,您的代码应该可以工作。因为当你设置 [imgview setUserInteractionEnabled:YES];那么所有的触摸都将由 UIImageView 处理,我假设你的 ImageView 的大小等于视图的大小。
It seems you are adding guestureRecognizer to superView of UIImageView but you are using this as well [imgview setUserInteractionEnabled:YES];. Comment this line and your code should work. Because when you set [imgview setUserInteractionEnabled:YES]; then all the touches will be handled by the UIImageView and I assume your ImageView's size is equal to view's size.