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