关于触摸移动功能

发布于 2024-11-19 07:12:00 字数 1392 浏览 2 评论 0原文

我有五张图片,它们被命名为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2024-11-26 07:12:00

看来您正在将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文