interface AppDelegate : UIResponder <UIApplicationDelegate, UIScrollViewDelegate>
@property (nonatomic, retain) UIWindow* window;
@property (nonatomic, retain) UIImageView* mapImageView;
@end
#import "AppDelegate.h"
@implementationAppDelegate
@synthesize window = _window;
@synthesize mapImageView = _mapImageView;
- (void)dealloc
{
self.mapImageView = nil;
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//创建图片视图,显示整张北京地图
_mapImageView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"BeijingMap.png"]];
//创建一个滚动视图
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:
[UIScreen mainScreen].applicationFrame];
//设置滚动视图要显示的整个地图的尺寸
scrollView.contentSize = self.mapImageView.frame.size;
//滚动视图的初始查看位置为地图的正中央
scrollView.contentOffset = CGPointMake(
(self.mapImageView.frame.size.width - scrollView.frame.size.width)/2,
(self.mapImageView.frame.size.height - scrollView.frame.size.height)/2);
//计算横纵方向上的缩放比例
float ratioX = scrollView.frame.size.width/self.mapImageView.frame.size.width;
float ratioY = scrollView.frame.size.height/self.mapImageView.frame.size.height;
//设置滚动视图的最小与最大缩放比例
scrollView.minimumZoomScale = ratioX > ratioY?ratioX: ratioY;
scrollView.maximumZoomScale = 2;
//设置滚动视图的委托对象
scrollView.delegate = self;
[scrollView addSubview:self.mapImageView];
[self.window addSubview:scrollView];
[scrollView release];
[self.window makeKeyAndVisible];
return YES;
}
//返回待缩放的视图
- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView
{
return self.mapImageView;
}
@end