以uiscrollview实现的图片放大效果

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

先上代码:

-(void)loadView
{
    [super loadView];
    UIView* view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    view.backgroundColor=[UIColor whiteColor];
    
    self.view=view;
    _scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];//申明一个scrollview
    _scrollView.delegate=self;//需要在.h中引用scrollview的delegate
    _scrollView.backgroundColor=[UIColor purpleColor];
    _scrollView.alpha=1.0;
    
    _imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1730, 2430)];//申明一个imageview
    _imageView.image=[UIImage imageNamed:@"WeCanDoIt"];
    UITapGestureRecognizer* tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesAction:)];//给imageview添加tap手势
    _imageView.userInteractionEnabled=YES;
    [_scrollView addSubview:_imageView];//需要将imageview添加到scrollview上
    [self.view addSubview: _scrollView];
    [_imageView addGestureRecognizer:tap];
    
    float minimumScale = [_scrollView frame].size.width / [_imageView frame].size.width;//设置缩放比例
    [_scrollView setMinimumZoomScale:minimumScale];//设置最小的缩放大小
    [_scrollView setZoomScale:minimumScale];//设置scrollview的缩放
//    [_scrollView setMaximumZoomScale:10000000.0];//设置最大的缩放大小
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tapGesAction:(UIGestureRecognizer*)gestureRecognizer//手势执行事件
{
    float newscale=0.2*1.5;
    CGRect zoomRect = [self zoomRectForScale:newscale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
    NSLog(@"zoomRect:%@",NSStringFromCGRect(zoomRect));
    [ _scrollView zoomToRect:zoomRect animated:YES];//重新定义其cgrect的x和y值
//    [_scrollView setZoomScale:newscale animated:YES];//以原先中心为点向外扩
}

-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView//scrollview的delegate事件。需要设置缩放才会执行。
{
    return _imageView;
}


- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {//传入缩放比例和手势的点击的point返回缩放后的scrollview的大小和X、Y坐标点
    
    CGRect zoomRect;
    
    // the zoom rect is in the content view's coordinates.
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [_scrollView frame].size.height / scale;
    zoomRect.size.width  = [_scrollView frame].size.width  / scale;
    
    // choose an origin so as to get the right center.
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
//    zoomRect.origin.x=center.x;
//    zoomRect.origin.y=center.y;
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);
    
    return zoomRect;
}

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



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值