0

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

代码如下:

-(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);
}
4

1 回答 1

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
    )
    
于 2016-08-03T12:55:38.200 回答