我尝试过使用框架制作动画视图。它会恢复为原始状态。导航栏会为此添加隐藏/显示的点击视图(点击手势)。
问题::施加捏合手势后,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
}
}
故事板设置
animateView
当前输出
预期产量