您的位置:首页 > 其它

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

2013-10-15 21:30 369 查看
在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.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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐