捏手势后如何将UIView动画化回原始帧

时间:2018-07-12 11:21:16

标签: ios uiview uinavigationcontroller uigesturerecognizer uiviewanimation

我尝试过使用框架制作动画视图。它会恢复为原始状态。导航栏会为此添加隐藏/显示的点击视图(点击手势)。

问题::施加捏合手势后,View会发生缩放。在点击视图时,它会重置为缩放的帧。无法理解为什么在点击后会重置视图框架。

添加代码以供参考

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var animateView: UIView!
    var originalFrame : CGRect = CGRect.zero

    override func viewDidLoad() {
        super.viewDidLoad()
        originalFrame = self.animateView.frame
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTapView(tapGestureRecognizer:)))
        tapGestureRecognizer.delegate = self
        self.view.addGestureRecognizer(tapGestureRecognizer)

        let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(recognizer:)))
        pinchGesture.delegate = self
        self.animateView.addGestureRecognizer(pinchGesture)
    }

    @IBAction func handlePinch(recognizer : UIPinchGestureRecognizer) {
        if let view = recognizer.view {
          view.transform = view.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
            print("Recogniser scale value::: \(recognizer.scale)")
          recognizer.scale = 1
        }
        if recognizer.state == UIGestureRecognizerState.ended {
            let viewFrame = animateView.frame
            //CGRectEqualToRect(viewBound,viewFrame)
            print("----------------------------------------------")

            if originalFrame.contains(viewFrame) {
                UIView.animate(withDuration: 0.5) {
                    UIView.setAnimationCurve(.easeIn)
                    self.animateView.frame = self.originalFrame
                }
            }
        }
    }

    @objc func didTapView(tapGestureRecognizer: UITapGestureRecognizer) {
        guard let isShow = self.navigationController?.isNavigationBarHidden else {
            return
        }
        self.navigationController?.isNavigationBarHidden = !isShow
    }
}

extension ViewController: UIGestureRecognizerDelegate {
  func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
  }
}

故事板设置

  1. 带导航控制器
  2. 附加另一个viewController
  3. 添加UIView并设置框架(0,64,320,504)
  4. 添加约束(引导,尾随,顶部,底部)
  5. 将UIView连接到animateView

当前输出

  1. 将视图缩小到一半大小
  2. 它将动画化为原始帧
  3. 点击彩色视图,视图进入步骤1的状态

预期产量

  1. 将视图缩小到一半大小
  2. 它将动画化为原始帧
  3. 点击彩色视图,无需调整大小。如当前输出所述

0 个答案:

没有答案