UIScrollView相关代理方法:
https://www.cnblogs.com/liuting-1204/p/5960815.html
iOS开发-图片查看(ScrollView+UIPageControl)
https://www.cnblogs.com/xiaofeixiang/p/4242919.html
ios开发scrollView中contentSize、contentInset和contentOffset区别
https://www.jianshu.com/p/e766f09f043b
//
// ViewController.m
// UIScrollView-滚动视图
//
// Created by Liu,Wenbo(TBRD) on 2020/5/29.
// Copyright © 2020 Liu,Wenbo(TBRD). All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UIScrollViewDelegate> //设置代理
{
UIScrollView *scrollview;
UILabel *lable;
UIImageView *imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
scrollview = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollview.backgroundColor = [UIColor grayColor];
UIImage *image = [UIImage imageNamed:@"test1.png"];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
imageView.image = image;
[scrollview addSubview:imageView];
lable = [[UILabel alloc] initWithFrame:CGRectMake(150, 150, 100, 100)];
lable.backgroundColor = [UIColor whiteColor];
//设置滚动视图的内容区域
scrollview.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height * 2);
//设置内容区域位置的偏移量 这里设置到了最右边
scrollview.contentOffset = CGPointMake(self.view.frame.size.width, 0);
//设置内容区域大小的偏移量 这里相当于多加一个屏幕的大小
scrollview.contentInset = UIEdgeInsetsMake(0, self.view.frame.size.width, 0, 0);
//设置代理
scrollview.delegate = self;
//方向锁定 先水平滑动则限定水平滑动(既不能垂直滑动);先垂直滑动的话就限定垂直滑动;但是对角线上没有限定
scrollview.directionalLockEnabled = YES;
//设置是否允许回弹
scrollview.bounces = NO;
//设置滑动提示线是否显示
//scrollview.showsVerticalScrollIndicator = YES;
//设置提示线的风格
scrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//设置scrollview的加速度
//scrollview.decelerationRate = 0.5;
//设置scrollview的缩放比
scrollview.minimumZoomScale = 0.5; // 最小缩0.5
scrollview.maximumZoomScale = 2; //最大扩大2
[scrollview addSubview:lable];
[self.view addSubview: scrollview];
}
//想缩放哪个控件就返回哪个控件
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return imageView;
}
@end
手势扩大或缩小按住option键即可操作