iOS - UIPinchGestureRecognizer& UIPanGestureRecognizer - 重置

时间:2013-06-20 02:40:54

标签: ios uipangesturerecognizer uipinchgesturerecognizer

在我的缩放和平移应用程序中,我正在使用上述手势识别器。这工作正常。 我想要一个按钮,将图像恢复到初始状态。这意味着显示实际图像或重置为初始状态。有人可以告诉我如何实现这个目标吗?

代码如下:

-(void)handlePanGesture:(UIPanGestureRecognizer*)recognizer

{
    CGPoint translation = [(UIPanGestureRecognizer*)recognizer translationInView:[self superview]];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
    [(UIPanGestureRecognizer*)recognizer setTranslation:CGPointMake(0, 0) inView:[self superview]];
}

-(void)handlePinchGesture:(UIPinchGestureRecognizer*)recognizer
{
    static CGRect initialBounds;

    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        initialBounds = self.bounds;
    }
    CGFloat factor = [(UIPinchGestureRecognizer *)recognizer scale];

    CGAffineTransform zt = CGAffineTransformScale(CGAffineTransformIdentity, factor, factor);
    self.bounds = CGRectApplyAffineTransform(initialBounds, zt);
}

1 个答案:

答案 0 :(得分:0)

基于@ borrden的评论。

  1. 检查ImageView和原始中心的当前中心是否相同。如果没有重置ImageView的中心。你可以添加一个UIView.animation ..让它看起来不错。
  2. 将imageView设置为 CGAffineTransformIdentity ,将其调整为原始大小。这也可以添加到上面的UIView.animation中。
  3. 代码。根据需要进行更改。

    UIView.animateWithDuration(0.2, delay: 0.0, options: .CurveEaseIn, animations: {
    
          //Move image back to center
          self.mainImageView.center = self.originalCenter!
          self.layoutIfNeeded()
    
          //Resize image to original
          self.mainImageView.transform = CGAffineTransformIdentity
    
          }, completion: nil
    )