iOS UIScrollView+UIImageView 制作简单的图片浏览器,支持缩放、平移

这篇博客介绍如何利用UIScrollView的内置功能,配合UIImageView,轻松实现一个支持缩放和平移的简单图片浏览器。博主详细讲解了代码实现过程,包括主界面及详细视图的设置,确保图片在缩放后能够正确居中显示,并处理了平移时的边界问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做一个简单的图片浏览器,支持缩放、平移。本想自己用手势处理图片的缩放和平移,但经过搜索引擎一搜索,发现可以借助UIScrollView的缩放功能,完美实现图片的缩放和平移。当前,中途也遇到缩放后图片没有居中显示,或者即使居中显示了,但是平移时发现图片一边到不了边,另一边却留很多空,找到了答案。下面我整理了下代码,发布出来.

首先是主界面代码;

#import "MainViewController.h"

#import "TowViewController.h"

#import "OneViewController.h"   

#import "MyButton.h"

@interface MainViewController ()


@end


@implementation MainViewController

-(void)dealloc

{

    

    [super dealloc];

}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        

    }return self;

}





- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];

    [self cover];


}

-(void)cover

{

    MyButton *aButton=[[MyButton alloc]initWithFrame:CGRectMake(20, 20, self.view.frame.size.width/3, self.view.frame.size.height/4)];

    UIImage *temp=[UIImage imageNamed:@"1.jpg"];

    [aButton setImage:temp forState:UIControlStateNormal];

    [aButton addTarget:self action:@selector(goToshanghai:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:aButton];

    [temp release];

    [aButton release];

    

    MyButton *bButton=[[MyButton alloc]initWithFrame:CGRectMake(20+aButton.frame.size.width,20,250, self.view.frame.size.height/4)];

//    [bButton setBackgroundColor:[UIColor redColor]];

    [bButton setTitle:@"小宝贝我来上海啦" forState:UIControlStateNormal];

    [bButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    bButton.titleLabel.font=[UIFont systemFontOfSize:20];

    [bButton addTarget:self action:@selector(goToshanghai:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:bButton];

    [bButton release];

    

    MyButton *aButton1=[[MyButton alloc]initWithFrame:CGRectMake(20, aButton.frame.size.height+40, self.view.frame.size.width/3, self.view.frame.size.height/4)];

    UIImage *temp1=[UIImage imageNamed:@"2.jpg"];

    [aButton1 setImage:temp1 forState:UIControlStateNormal];

    [aButton1 addTarget:self action:@selector(goToMydalian:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:aButton1];

    [temp1 release];

    [aButton1 release];

    MyButton *bButton1=[[MyButton alloc]initWithFrame:CGRectMake(20+aButton.frame.size.width,aButton.frame.size.height+40,250, self.view.frame.size.height/4)];

//    [bButton1 setBackgroundColor:[UIColor redColor]];

    [bButton1 setTitle:@"小宝贝来了大连啦" forState:UIControlStateNormal ];

    [bButton1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

     bButton1.titleLabel.font=[UIFont systemFontOfSize:20];

    [bButton1 addTarget:self action:@selector(goToMydalian:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:bButton1];

    [bButton1 release];

    MyButton *aButton2=[[MyButton alloc]initWithFrame:CGRectMake(20, aButton1.frame.origin.y+aButton.frame.size.height+20, self.view.frame.size.width/3, self.view.frame.size.height/4)];

    UIImage *temp2=[UIImage imageNamed:@"3.jpg"];

    [aButton2 setImage:temp2 forState:UIControlStateNormal];

    [aButton2 addTarget:self action:@selector(TheCloudOfMissing:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:aButton2];

    [temp2 release];

    [aButton2 release];

    MyButton *bButton2=[[MyButton alloc]initWithFrame:CGRectMake(20+aButton.frame.size.width,aButton1.frame.origin.y+aButton.frame.size.height+20,250, self.view.frame.size.height/4)];

//    [bButton2 setBackgroundColor:[UIColor redColor]];

    [bButton2 setTitle:@"思念描绘的云彩" forState:UIControlStateNormal];

    [bButton2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    bButton2.titleLabel.font=[UIFont systemFontOfSize:25];

    [bButton2 addTarget:self action:@selector(TheCloudOfMissing:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:bButton2];

    [bButton2 release];

    

    

}

-(void)goToshanghai:(id)sender

{

    OneViewController *One=[[OneViewController alloc]init];

    [self presentViewController:One animated:YES completion:^{

        

        

    }];

    [One  release];

}



-(void)goToMydalian:(id)sender

{

   TowViewController *Tow=[[TowViewController alloc]init];

    [self presentViewController:Tow animated:YES completion:^{

        

        

    }];

    [Tow release];

}


然后是进入相册之后进行循环自动播放的代码在另一类中建立的

:

#import "OneViewController.h"

#import "MyButton.h"

@interface OneViewController ()


@end


@implementation OneViewController

-(void)dealloc

{

  

    [super dealloc];

}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        

    }return self;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    [self goToshanghai];

}

-(void)goToshanghai

{

    UIView *aView=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

    [aView setBackgroundColor:[UIColor whiteColor]];

    [self.view addSubview:aView];

    [aView release];

    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20,aView.frame.size.width, 550)];

    [_imageView setImage:[UIImage imageNamed:@"11.JPG"]];

    _imageView.animationDuration=15;

    [aView addSubview: _imageView];

    _imageView.userInteractionEnabled = YES;

    [_imageView release];

    

    

    MyButton *button  =[[MyButton alloc]initWithFrame:CGRectMake(30, 600, 50,30)];

    button.backgroundColor = [UIColor yellowColor];

    [button setTitle:@"开始" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    button.selected = YES;

    [button addTarget:self action:@selector(startAnimating) forControlEvents:UIControlEventTouchUpInside];

    [aView addSubview:button];

    [button release];

    MyButton *button2 = [[MyButton alloc]initWithFrame:CGRectMake(130,600, 50, 30)];

    button2.backgroundColor = [UIColor yellowColor];

    [button2 setTitle:@"停止" forState:UIControlStateNormal];

    [button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    button2.selected = YES;

    [button2 addTarget:self action:@selector(stopAnimating) forControlEvents:UIControlEventTouchUpInside];

    [aView addSubview: button2];

    [button2 release];

    MyButton *button3 = [[MyButton alloc]initWithFrame:CGRectMake(230,600, 50, 30)];

    button3.backgroundColor = [UIColor yellowColor];

    [button3 setTitle:@"返回" forState:UIControlStateNormal];

    [button3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    button3.selected = YES;

    [button3 addTarget:self action:@selector(goToshanghai:) forControlEvents:UIControlEventTouchUpInside];

    [aView addSubview: button3];

    [button3 release];

    

    

    

    NSMutableArray *array = [NSMutableArray array];

    for(int i =1;i< 22;i++){

        NSString *name = [NSString stringWithFormat:@"1%d.JPG",i];

        UIImage *image = [UIImage imageNamed:name];

        [array addObject:image];

        

    }

    _imageView.animationImages = array;

    

}



-(void)sliderAction:(id)sender{

    

    [self.imageView startAnimating];

}



- (void)startAnimating{

    [self.imageView startAnimating];

}

- (void)stopAnimating{

    [self.imageView stopAnimating];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(void)goToshanghai:(id)sander

{

    [self dismissViewControllerAnimated:YES completion:nil];

}


最后是第二个相册的代码内容,是进行手动的翻动照片产生的效果与iPhone手机上的效果基本是一样一样的;
能放大能缩小,能循环翻页;

#import "TowViewController.h"

#import "MyButton.h"

@interface TowViewController ()<UIScrollViewDelegate>

@property(nonatomic ,assign)UIScrollView *aScroll;

@property(nonatomic ,assign)UIPageControl *page;

@end


@implementation TowViewController

-(void)dealloc

{

    [_aScroll release];

    [super dealloc];

}

-(void)loadView

{

    [super loadView];

}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        

    }return self;

}









- (void)viewDidLoad {

    [super viewDidLoad];

    [self MyView];

}

-(void)MyView


{

    

    self.aScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 500)];

    _aScroll.delegate=self;

    [_aScroll setBackgroundColor:[UIColor yellowColor]];

    _aScroll.pagingEnabled=YES;//边界回弹;

    

    

    _aScroll.maximumZoomScale=2.0f;

    _aScroll.minimumZoomScale=0.5f;

    _aScroll.contentOffset=CGPointMake(_aScroll.frame.size.width*0, 0);

    

    [_aScroll setContentSize:CGSizeMake(_aScroll.frame.size.width*14,0)];

    [self.view addSubview:_aScroll];

    

    

    for (int i=1; i<15; i++) {

        NSString *name=[NSString stringWithFormat:@"0%d",i];

        

        NSString *path=[[NSBundle mainBundle]pathForResource:name ofType:@"JPG"];

        UIImage *myimage=[UIImage imageWithContentsOfFile:path];

        

        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _aScroll.frame.size.width, _aScroll.frame.size.height)];

        [imageView setImage:myimage];

        UIScrollView *bScroll=[[UIScrollView alloc]initWithFrame:CGRectMake((i-1)*_aScroll.frame.size.width, 0, _aScroll.frame.size.width, _aScroll.frame.size.height)];

        [bScroll setBackgroundColor:[UIColor blackColor]];

        bScroll.maximumZoomScale=2.0f;

        bScroll.minimumZoomScale=0.5f;

        bScroll.delegate=self;

        [_aScroll addSubview:bScroll];

        [bScroll addSubview:imageView];

        [imageView release];

    }

    UIScrollView *cScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(-_aScroll.frame.size.width,0,_aScroll.frame.size.width,_aScroll.frame.size.height)];

    UIImageView *aimage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cScroll.frame.size.width, cScroll.frame.size.height)];

    [aimage setImage:[UIImage imageNamed:@"014.JPG"]];

    [_aScroll addSubview:cScroll];

    [cScroll addSubview:aimage];

    UIScrollView *dScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(_aScroll.frame.size.width*14,0,_aScroll.frame.size.width,_aScroll.frame.size.height)];

    UIImageView *bimage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cScroll.frame.size.width, dScroll.frame.size.height)];

    [bimage setImage:[UIImage imageNamed:@"01.JPG"]];

    [_aScroll addSubview:dScroll];

    [dScroll addSubview:bimage];

    self.page=[[UIPageControl alloc]initWithFrame:CGRectMake(0, 530, 380, 40)];

    [_page setBackgroundColor:[UIColor blackColor]];

    _page.numberOfPages=14;

    [_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:_page];

    [_page release];

    

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(230,600, 50, 30)];

    button.backgroundColor = [UIColor yellowColor];

    [button setTitle:@"返回" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    button.selected = YES;

    [button addTarget:self action:@selector(loginInterface:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview: button];

    [button release];

   

}


-(void)pageAction:(id)sender

{

    CGFloat value =_page.currentPage* self.view.frame.size.width;

    [_aScroll setContentOffset:CGPointMake(value, 0) animated:YES];

}



-(void)loginInterface:(id)sander

{

    [self dismissViewControllerAnimated:YES completion:nil];

}




- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}






-(void)scrollViewDidScroll:(UIScrollView *)scrollView//正在进行滚动运行

{

    NSLog(@"%s",__func__);

    

    CGPoint str=self.aScroll.contentOffset;

    if (str.x<0 ) {

        [self.aScroll setContentOffset:CGPointMake(self.aScroll.frame.size.width*13, 0) animated:YES];

    }

    if (str.x>13*375) {

        [self.aScroll setContentOffset:CGPointMake(0, 0) animated:YES];

    }

    

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView//将要开始滚动运行;

{

    

}


-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset//已经结束滚动运行

{

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate //将要结束滚动运行;

{

    

}

-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    NSArray *array=scrollView.subviews;

    return [array objectAtIndex:0];

}


- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView

{


}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    CGPoint temp=self.aScroll.contentOffset;

    self.page.currentPage=temp.x/self.aScroll.frame.size.width;

    if (temp.x<0) {

        self.page.currentPage=13;

    }

    if (temp.x>

        375*13) {

        self.page.currentPage=0;

    }


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值