SoFunction
Updated on 2025-04-10

iOS tableView achieves pull-down image enlargement effect

This article shares the specific code for iOS to realize the zoom-in effect of pull-down image for your reference. The specific content is as follows

#import ""
#define kScreenbounds [UIScreen mainScreen].bounds
#define kScreenWidth [UIScreen mainScreen].
#define kScreenHeight [UIScreen mainScreen].
// Macro defines a height#define pictureHeight 200
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *pictureImageView;
@property (nonatomic, strong) UIView *header;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  
   = @"Stretch down to enlarge the picture";
  // The settings of the following two properties are the same as the effect of transforming is NO and coordinate transformation   = UIRectEdgeNone;
   = NO;
  [self createTableView];
  
}
- (void)createTableView
{
   = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
  _tableView.delegate = self;
  _tableView.dataSource = self;
  [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
  
  // Add head view Add ImageView to head view   = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, pictureHeight)];
  _pictureImageView = [[UIImageView alloc] initWithFrame:_header.bounds];
  _pictureImageView.image = [UIImage imageNamed:@"picture"];
  /*
    Important property settings
    */
  //The value of this property determines how to reuse the view's content when the geometry of the view changes. Here, UIViewContentModeScaleAspectFill is used to maintain the content aspect ratio and scale the content. Some content beyond the view will be cut. Fill the UIView  _pictureImageView.contentMode = UIViewContentModeScaleAspectFill;
  // This property determines the display range of the child view. When the value is YES, the child view part beyond the range of the parent view is cut. Here is the part where the _pictureImageView exceeds the range of the _header is cut.  _pictureImageView.clipsToBounds = YES;
  [_header addSubview:_pictureImageView];
   = _header;
  [ addSubview:_tableView];
  
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

  return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
   = @"Pull me down";
  return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  
  /**
    * The offset here is calculated vertically from contentInset, then the offset is 0 at the beginning, negative downwards, positive upwards, pull down
    */
  
  // Get the tableView offset  CGFloat Offset_y = ;
  // Pull down The vertical offset becomes smaller and becomes negative    if ( Offset_y < 0) {
      // The height of the picture after stretching      CGFloat totalOffset = pictureHeight - Offset_y;
      // Picture enlargement ratio      CGFloat scale = totalOffset / pictureHeight;
      CGFloat width = kScreenWidth;
      // The picture position after stretching      _pictureImageView.frame = CGRectMake(-(width * scale - width) / 2, Offset_y, width * scale, totalOffset);
    }

}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

@end

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.