SoFunction
Updated on 2025-04-13

iOS uses UIScorllView to achieve two-finger scaling function

The two-finger scaling function can not only be implemented using the UIPinchGestureRecognizer gesture, but also using UIScorllView. UIScrollView can easily achieve the maximum and minimum scaling values ​​and scrolling effects. The code is as follows:

#import ""
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  _scrollView.minimumZoomScale = 1.0;   // Minimum scaling value  _scrollView.maximumZoomScale = 10.0;  // Maximum scaling value  [_scrollView setZoomScale:_scrollView.minimumZoomScale];  // The initial scaling value  _scrollView.delegate = self;
  _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
  _imageView.frame = ;
  [_scrollView addSubview:_imageView];
}
#pragma mark - Returns the control that needs to be scaled- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  return _imageView;
}
- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
}
@end

The key point is to call the viewForZoomingInScrollView: proxy method, which returns the control that needs to be scaled.

Demo's GitHub address

The above is what the editor introduced to you about using UIScorllView to implement the two-finger scaling function of iOS. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!