gpt4 book ai didi

ios - iOS 中,当用户通过 UISwipeGestureRecognizer 在 ImageView 中向左滑动时,如何在 ImageView 中显示 NextImage?

转载 作者:行者123 更新时间:2023-11-29 02:29:22 26 4
gpt4 key购买 nike

我是 iOS 开发新手。我想在我的应用程序中制作一个应用程序,我制作了一个 UIScrollview,并在此 UIScrollView 中添加了一个 UIImageview。当 viewDidAppear 时,我将 imageview 图像设置为

-(void)viewDidAppear:(BOOL)animated
{
NSDictionary *dict=[self.imagesa objectAtIndex:0];
NSString *imagelink=[dict valueForKey:@"link"];
NSLog(@"imageLink %@",imagelink);
[self.bigImage sd_setImageWithURL:[NSURL URLWithString:imagelink] placeholderImage:[UIImage imageNamed:@"1.png"]];
}

以及为我的 Imageview 和 Scrollvie 添加手势的代码,例如

 UISwipeGestureRecognizer *swipeGestureLeftdirection=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(slideToLeftWithGestureRecognizer:)];
swipeGestureLeftdirection.delegate=self;
swipeGestureLeftdirection.direction=UISwipeGestureRecognizerDirectionLeft;
[self.bigImage addGestureRecognizer:swipeGestureLeftdirection];
[self.bigScrollview addGestureRecognizer:swipeGestureLeftdirection];

UISwipeGestureRecognizer *swipeGestureRightdirection=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(slideToRighttWithGestureRecognizer:)];
swipeGestureRightdirection.delegate=self;
swipeGestureRightdirection.direction=UISwipeGestureRecognizerDirectionRight;
[self.bigImage addGestureRecognizer:swipeGestureRightdirection];
[self.bigScrollview addGestureRecognizer:swipeGestureRightdirection];

这里的 Self.bigimage 是我的 UIImageview,self.bigScrollview 是我的 UIScrollView。现在我想当用户向左滑动时我想要 UIImageview 图像作为 self.imagesa 下一个索引,当向右滑动时我想要 UIImageVIew 图像返回索引图像如何可能。请给我解决方案。

最佳答案

不要在里面使用循环,slideToLeftWithGestureRecognizer:

这样写代码

 -(void)slideToLeftWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer 
{
if(index<[self.imagesa count])
{
NSDictionary *dict=[self.imagesa objectAtIndex:index];
NSString *imagelink=[dict valueForKey:@"link"];
NSLog(@"imageLink %@",imagelink);
[self.bigImage sd_setImageWithURL:[NSURL URLWithString:imagelink] placeholderImage: [UIImage imageNamed:@"1.png"]];

index++;
}
}

使索引成为全局变量,在 viewDidLoad 上初始化为 0。

  -(void)slideToRighttWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer 
{
if(index>0)
{
index--;
NSDictionary *dict=[self.imagesa objectAtIndex:index];
NSString *imagelink=[dict valueForKey:@"link"];
NSLog(@"imageLink %@",imagelink);
[self.bigImage sd_setImageWithURL:[NSURL URLWithString:imagelink] placeholderImage: [UIImage imageNamed:@"1.png"]];


}
}

关于ios - iOS 中,当用户通过 UISwipeGestureRecognizer 在 ImageView 中向左滑动时,如何在 ImageView 中显示 NextImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27099019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com