做一个简单的图片浏览器,支持缩放、平移。本想自己用手势处理图片的缩放和平移,但经过搜索引擎一搜索,发现可以借助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];
}
#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;
}
}