Search code examples
iosswiftuikit

scaling a UIView without scaling any of the subviews


i have a container view that contains another smaller view inside i want to scale the container view without the other view being affected i am using just a longGestureRecognizer to detect the long hold then using UIView.animate(withDuration:1) {} and using CGAffineTransform to scale the view up here's is the code for both of the views '''

  let viewIWantToScale = UIView()
  let viewIDontWantToScale = UIView()

  private func configureViewIWantToScale() {
    let tabDetector = UILongPressGestureRecognizer(target: self, action: #selector(gestureAction(_:)))
    viewIWantToScale.translatesAutoresizingMaskIntoConstraints = false;
    viewIWantToScale.addGestureRecognizer(tabDetector)
    viewIWantToScale.autoresizesSubviews = false

    NSLayoutConstraint.activate([
    
        viewIWantToScale.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        viewIWantToScale.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        viewIWantToScale.widthAnchor.constraint(equalToConstant: 300),
        viewIWantToScale.heightAnchor.constraint(equalToConstant: 300)
        
    ])
    
    viewIWantToScale.backgroundColor = .blue
    
}

private func configureViewIDontWantToScale() {
    viewIDontWantToScale.translatesAutoresizingMaskIntoConstraints = false;
    viewIDontWantToScale.isUserInteractionEnabled = false
    NSLayoutConstraint.activate([
    
        viewIDontWantToScale.centerYAnchor.constraint(equalTo: viewIWantToScale.centerYAnchor),
        viewIDontWantToScale.centerXAnchor.constraint(equalTo: viewIWantToScale.centerXAnchor),
        viewIDontWantToScale.widthAnchor.constraint(equalToConstant: 150),
        viewIDontWantToScale.heightAnchor.constraint(equalToConstant: 150)
        
    ])
    
    viewIDontWantToScale.backgroundColor = .red
    
}

'''


Solution

  • You can use inverted() function of CGAffineTransform. Add this line just after transforming your viewIWantToScale.transform line:

    self.viewIDontWantToScale.transform = TRANSFORM_YOU_USED.inverted()