I have added the images to scrollview dynamically. Following is the code:
-(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];
}
It is showing the images well when I use 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];
}
but when I used touch event to imageview then it is not displaying the images smoothly. It gets stuck during scrolling.
These are the preview which shows the current image with the start of second image.
The two methods: scrollViewWillBeginDecelerating and scrollViewDidEndDecelerating contains two animation with different x position it is animated to:
frame.origin.x = frame.size.width-15*gallerypage;
and
frame.origin.x=gallerypage*290;
It is better if you could turn off either function: scrollViewWillBeginDecelerating or scrollViewDidEndDecelerating. Otherwise probably you should adjust the x position (must be in sync for both functions) it will be animated to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With