xcode uiscrollview with paging and zoom
Here is a simple and sample code that demonstrate the photo slide function with zoom using uiscrollview.
this is the first photo
sliding
zooming in particular photo
1. first drag three photos to your project , size not important .
2 viewcontroller.h 's code
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIScrollViewDelegate>{
UIScrollView *bgScorllView; // background scrollview control the slide paging
UIView *bgView; // background view is on the bgscrollview
UIImageView *picImageView; // the imageview for the photo where in the current screen
UIScrollView *picScrollView; // the scrollview for the current screen to preform zoom function
UIImageView *preImageView; // the imageview for the left side of the current screen
UIImageView *nextImageView; // the imageview for the right side of the current screen
}
@end
3. code for viewcontroller.m
at viewdidload
1. define the contentsize of the view , it 's for sliding and paging on the bgscrollview
2. run loadPage
at loadPage
1. remove all subview at bgview first to save memory
2. load the current screen scroll view and imagview
3. add imageview at both sides of current screen , so even slide before reach that page , can see that images
4. add tag for the current screen scrollview , so it won't be confused for uiscrollview delegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
this is the first photo
sliding
zooming in particular photo
1. first drag three photos to your project , size not important .
2 viewcontroller.h 's code
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIScrollViewDelegate>{
UIScrollView *bgScorllView; // background scrollview control the slide paging
UIView *bgView; // background view is on the bgscrollview
UIImageView *picImageView; // the imageview for the photo where in the current screen
UIScrollView *picScrollView; // the scrollview for the current screen to preform zoom function
UIImageView *preImageView; // the imageview for the left side of the current screen
UIImageView *nextImageView; // the imageview for the right side of the current screen
}
@end
3. code for viewcontroller.m
at viewdidload
1. define the contentsize of the view , it 's for sliding and paging on the bgscrollview
2. run loadPage
at loadPage
1. remove all subview at bgview first to save memory
2. load the current screen scroll view and imagview
3. add imageview at both sides of current screen , so even slide before reach that page , can see that images
4. add tag for the current screen scrollview , so it won't be confused for uiscrollview delegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
#import "ViewController.h"
@implementation ViewController
int numberOfPhotos = 3;
int currentpage;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
-(void)loadPage :(int) page{
currentpage = page;
for (UIImageView *sView in bgView.subviews){
NSLog(@"removesubview %@",sView);
[sView removeFromSuperview];
}
NSLog(@"page number %d",page);
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"iphonescroll%d.png",page]];
picScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((page-1)*320+10, 10,300,440)];
picScrollView.delegate = self;
picScrollView.maximumZoomScale =3;
picScrollView.minimumZoomScale =1;
picScrollView.zoomScale =1;
picScrollView.clipsToBounds = YES;
picScrollView.bounces = YES;
picScrollView.scrollEnabled = YES;
picScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
picScrollView.tag = 5;
picImageView = [[UIImageView alloc] initWithImage:image];
[picImageView setFrame:CGRectMake(0, 0, 300,440)];
[picScrollView addSubview:picImageView];
[bgView addSubview:picScrollView];
int nextpage = page +1 ;
UIImage *nextimage = [UIImage imageNamed:[NSString stringWithFormat:@"iphonescroll%d.png",nextpage]];
nextImageView = [[UIImageView alloc] initWithImage:nextimage];
[nextImageView setFrame:CGRectMake((nextpage-1)*320+10, 10, 300,440)];
[bgView addSubview:nextImageView];
int prepage = page -1 ;
UIImage *preimage = [UIImage imageNamed:[NSString stringWithFormat:@"iphonescroll%d.png",prepage]];
preImageView = [[UIImageView alloc] initWithImage:preimage];
[preImageView setFrame:CGRectMake((prepage-1)*320+10, 10, 300,440)];
[bgView addSubview:preImageView];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"%@",[UIScreen mainScreen]);
bgScorllView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view addSubview:bgScorllView];
int width = 320*numberOfPhotos;
bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width,460)];
[self loadPage:1];
[bgScorllView setContentSize:CGSizeMake(width,460)];
bgScorllView.pagingEnabled = YES;
bgScorllView.delegate = self;
bgScorllView.backgroundColor = [UIColor blueColor];
[bgScorllView addSubview:bgView];
//[self.view addSubview:bgView];
}
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
//NSLog(@"viewforzooming scrollview tag %d",scrollView.tag);
return picImageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
//NSLog(@"scrollviewdidzoom scrollview tag %d %f",scrollView.tag,scrollView.zoomScale);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView.tag == 0){
//NSLog(@"viewdisscroll length %f",scrollView.contentOffset.x);
int pageNumber = floor(scrollView.contentOffset.x / 320 + 0.5) +1;
//NSLog(@"page number %d",pageNumber);
if (pageNumber != currentpage){
[self loadPage:pageNumber];
}
}
}
/*
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//NSLog(@"scrollviewdidscroll scrollview tag %d",scrollView.tag);
if (scrollView.tag == 0){
//NSLog(@"viewdisscroll length %f",scrollView.contentOffset.x);
int pageNumber = floor(scrollView.contentOffset.x / 320 + 0.5) +1;
//NSLog(@"page number %d",pageNumber);
[self loadPage:pageNumber];
}
}
*/
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
if (scrollView.tag == 0 ){
// NSLog(@"viewdidendscroll ");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix