当前位置:编程学习 > wap >>

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

 

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

 

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

 

@inte易做图ce TestBedViewController : UIViewController <UIScrollViewDelegate>

 

{

 

UIImage *weathermap;

 

}

 

@property (retain) UIImage *weathermap;

 

@end

 

 

@implementation TestBedViewController

 

@synthesize weathermap;

 

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

 

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

 

{

 

return [self.view viewWithTag:201];

 

}

 

 

/*

 

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

 

{

 

}

 

*/

 

 

- (void) viewDidLoad

 

{

 

 

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

 

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

 

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

 

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

 

 

// 创建图片对象

 

UIImageView *iv = [[[UIImageView alloc] 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.view addSubview:sv];

 

}

 

@end

 

作者sdhjob

补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,