ipad实现ScrollView通过手势滚动和缩放的Image

来源:互联网 发布:淘宝淘金币怎么没有了 编辑:程序博客网 时间:2024/05/21 10:36

在ipad上预览一张图片的时候,如果我们希望能够够缩放和滚动(类似与google地图效果),需要使用ScrollView

-------视图控制器定义如下

@interface TestBedViewController :UIViewController <UIScrollViewDelegate>

{

UIImage *weathermap;

}

@property (retain) UIImage *weathermap;

@end


@implementation TestBedViewController

@synthesize weathermap;

//在通过缩放手势的时候制定,所有ScrollView中的Image

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

{

return [self.viewviewWithTag:201];

}


/*

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale

{

}

*/


- (void) viewDidLoad

{

// 创建滚动视图并设置大小和代理对象                                                        px    py   宽度 高度

UIScrollView *sv = [[[UIScrollViewalloc] initWithFrame:CGRectMake(0.0f,0.0f, 320.0f,284.0f)] autorelease];

sv.contentSize =self.weathermap.size;//要缩放的UIImage对象

sv.delegate = self; //设定代理对象

// 创建图片对象

UIImageView *iv = [[[UIImageViewalloc] initWithImage:self.weathermap]autorelease];

iv.userInteractionEnabled =YES;

iv.tag = 201;

// 计算缩放数值

float minzoomx = sv.frame.size.width /self.weathermap.size.width;

float minzoomy = sv.frame.size.height /self.weathermap.size.height;

sv.minimumZoomScale = MIN(minzoomx, minzoomy);  //最小缩放到当前ScrollView的大小比例

sv.maximumZoomScale =3.0f; //最大缩放到图片的3倍


// 在scorllView添加image

[sv addSubview:iv];

[self.viewaddSubview:sv];

}

@end


原创粉丝点击