内置视图-滚动视图

来源:互联网 发布:打印出库单软件 编辑:程序博客网 时间:2024/05/22 16:06

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 = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];

    //创建图片视图,显示整张北京地图

    _mapImageView = [[UIImageViewalloc] initWithImage:

                     [UIImage imageNamed:@"BeijingMap.png"]];

    

    //创建一个滚动视图

    UIScrollView* scrollView = [[UIScrollViewalloc] initWithFrame:

                                [UIScreenmainScreen].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.windowaddSubview:scrollView];

    [scrollView release];

    

    [self.windowmakeKeyAndVisible];

    return YES;

}

 

//返回待缩放的视图

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

{

    return self.mapImageView;

}

@end
0 0
原创粉丝点击