gpt4 book ai didi

ios - ImageView 的平滑滚动

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:21:22 26 4
gpt4 key购买 nike

我已将图像动态添加到 ScrollView 。以下是代码:

-(void)displayimages {
for (int i=0; i<[featuredarray count]; i++) {
NSString *image=[[featuredarray objectAtIndex:i]objectForKey:@"img"];

if (i==0) {
webview=[[UIImageView alloc]initWithFrame:CGRectMake(i*290+15, 0, 270, 380)];
}
else {
webview=[[UIImageView alloc]initWithFrame:CGRectMake(i*290+15, 0, 270, 380)];
}
webview.backgroundColor=[UIColor clearColor];
webview.layer.borderWidth=4;
webview.layer.cornerRadius=4;
webview.layer.borderColor=[[UIColor whiteColor]CGColor];
[webview setTag:i];
webview.userInteractionEnabled=YES;
NSURL *url = [NSURL URLWithString:image];
[webview setImageWithURL:url placeholderImage:[UIImage
imageNamed:@"placeholder.png"]];
[scroll addSubview:webview];

UITapGestureRecognizer *tap1 =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlefirstTap:)];
tap1.delegate=self;
tap1.numberOfTapsRequired=1;
[webview addGestureRecognizer:tap1];
[tap1 release];
[webview release];
}

pages.defersCurrentPageDisplay=YES;
scroll.delegate =self;

scroll.contentSize = CGSizeMake(290*[featuredarray count],300);
scroll.pagingEnabled = YES;
pages.numberOfPages = [featuredarray count]-1;
pages.currentPage =0;
}

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
int gallerypage = scrollView.contentOffset.x /scrollView.frame.size.width;
CGRect frame = scroll.frame;
frame.origin.x = frame.size.width-15*gallerypage;
NSLog(@"frame.origin.x..%f",frame.origin.x);
frame.origin.y =0;
[scroll scrollRectToVisible:frame animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int gallerypage = scrollView.contentOffset.x /scrollView.frame.size.width;
CGRect frame = scroll.frame;
frame.origin.x=gallerypage*290;
frame.origin.y =0;
[scroll scrollRectToVisible:frame animated:YES];
}

当我使用 pagecontroller action 时它显示图像很好

-(IBAction)pagecontrolaction:(id)sender {
int page = pages.currentPage;
CGRect frame = scroll.frame;
frame.origin.x=page*290;
frame.origin.y =0;
[scroll scrollRectToVisible:frame animated:YES];
}

但是当我对 imageview 使用触摸事件时,它无法流畅地显示图像。它在滚动过程中卡住。

这些是显示当前图像和第二张图像开始的预览。

最佳答案

这两种方法:scrollViewWillBeginDecelerating 和 scrollViewDidEndDecelerating 包含两个具有不同 x 位置的动画:

frame.origin.x = frame.size.width-15*gallerypage;

frame.origin.x=gallerypage*290;

最好关闭其中一个函数:scrollViewWillBeginDecelerating 或 scrollViewDidEndDecelerating。否则,您可能应该调整动画的 x 位置(必须对两个函数同步)。

关于ios - ImageView 的平滑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17336450/

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