以uiscrollview实现的图片放大效果

转自:http://blog.csdn.net/sinyran/article/details/8261776


在苹果例子中有实例ScrollViewSuite,可以去文档的(sample code)例子代码中搜索下。

先上代码:

[plain]  view plain  copy
  1. -(void)loadView  
  2. {  
  3.     [super loadView];  
  4.     UIView* view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  5.     view.backgroundColor=[UIColor whiteColor];  
  6.       
  7.     self.view=view;  
  8.     _scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];//申明一个scrollview  
  9.     _scrollView.delegate=self;//需要在.h中引用scrollview的delegate  
  10.     _scrollView.backgroundColor=[UIColor purpleColor];  
  11.     _scrollView.alpha=1.0;  
  12.       
  13.     _imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1730, 2430)];//申明一个imageview  
  14.     _imageView.image=[UIImage imageNamed:@"WeCanDoIt"];  
  15.     UITapGestureRecognizer* tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesAction:)];//给imageview添加tap手势  
  16.     _imageView.userInteractionEnabled=YES;  
  17.     [_scrollView addSubview:_imageView];//需要将imageview添加到scrollview上  
  18.     [self.view addSubview: _scrollView];  
  19.     [_imageView addGestureRecognizer:tap];  
  20.       
  21.     float minimumScale = [_scrollView frame].size.width / [_imageView frame].size.width;//设置缩放比例  
  22.     [_scrollView setMinimumZoomScale:minimumScale];//设置最小的缩放大小  
  23.     [_scrollView setZoomScale:minimumScale];//设置scrollview的缩放  
  24. //    [_scrollView setMaximumZoomScale:10000000.0];//设置最大的缩放大小  
  25. }  
  26.   
  27. - (void)viewDidLoad  
  28. {  
  29.     [super viewDidLoad];  
  30.     // Do any additional setup after loading the view, typically from a nib.  
  31. }  
  32.   
  33. - (void)didReceiveMemoryWarning  
  34. {  
  35.     [super didReceiveMemoryWarning];  
  36.     // Dispose of any resources that can be recreated.  
  37. }  
  38.   
  39. -(void)tapGesAction:(UIGestureRecognizer*)gestureRecognizer//手势执行事件  
  40. {  
  41.     float newscale=0.2*1.5;  
  42.     CGRect zoomRect = [self zoomRectForScale:newscale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];  
  43.     NSLog(@"zoomRect:%@",NSStringFromCGRect(zoomRect));  
  44.     [ _scrollView zoomToRect:zoomRect animated:YES];//重新定义其cgrect的x和y值  
  45. //    [_scrollView setZoomScale:newscale animated:YES];//以原先中心为点向外扩  
  46. }  
  47.   
  48. -(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView//scrollview的delegate事件。需要设置缩放才会执行。  
  49. {  
  50.     return _imageView;  
  51. }  
  52.   
  53.   
  54. - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {//传入缩放比例和手势的点击的point返回<span style="color:#ff0000;">缩放</span>后的scrollview的大小和X、Y坐标点  
  55.       
  56.     CGRect zoomRect;  
  57.       
  58.     // the zoom rect is in the content view's coordinates.  
  59.     //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.  
  60.     //    As the zoom scale decreases, so more content is visible, the size of the rect grows.  
  61.     zoomRect.size.height = [_scrollView frame].size.height / scale;  
  62.     zoomRect.size.width  = [_scrollView frame].size.width  / scale;  
  63.       
  64.     // choose an origin so as to get the right center.  
  65.     zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);  
  66. //    zoomRect.origin.x=center.x;  
  67. //    zoomRect.origin.y=center.y;  
  68.     zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);  
  69.       
  70.     return zoomRect;  
  71. }  

还有在试的时候未设置俩个手指的缩放的,按alt加鼠标的还是可以将图片的大小进行放大缩小的这是什么原因?我看苹果的例子中时有对俩个手指的事件进行判读的执行缩小动作的。模拟机上可以。不知道真机上是否一样。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值