IOS 怎么用UIScrollView来滚动和缩放他的内容第一篇

 我来答
地瓜说机
2015-11-13 · TA获得超过2.9万个赞
知道大有可为答主
回答量:2.3万
采纳率:91%
帮助的人:1.2亿
展开全部
UIScrollViewIOS用控件展现超屏幕内容式面技巧使用

  篇文章关于UIScrollView深入浅看看我接习内容:

  1:用UIScrollView展比较图片

  2:UIScrollView缩放候直保持间

  3:UIScrollView面嵌入复杂视图层

  4:UIScrollView特性UIPageControl起浏览页面内容

  5:创建UIScrollView滚视图面能看页页部并且能看前页面像appstore浏览app效

  篇文章IOS5.0 xcode4.5环境

  我始创建项目图:

  Image

  我填项目名字创建appid候写公司标识类名字前缀设置我设备iPhone我暂支持iPhone模式选择单视图模版选择步并且选择保存位置

  由于我介绍UIScrollView4效我创建tableView每cell现新视图控制器并且展现效

  Image(1)

  面图显示现storyboard完候

  我编译UITableView导航接我要做:

  1:打MainStroyboard.storyboard并且点击系统模版给我创建第初始化场景

  2:我添加UITableViewController象库放storyboard

  3:现选择tableView刚才添加选择EditorEmbed inNavigationController

  4:选择tableViewControllertableView并且设置cell类型静态类型属性检查器

  5:设置tableViewsection4cell设置cellbasic类型lables改Image scrollCustom View scrollpaged paged with peeking

  Image(2)

  保存storyboard 并且编译运行应该看tableView图:

  Image(3)

  滚缩放图片:

  我接要做习用UIScrollView缩放滚图片

  第步需要设置Viewcontroller选择ViewController.h 并且添加UIScrollViewoutlet让Controller符合UIScrollViewUIScrollViewDelegate协议

  复制代码
  #import
  
  @interface ViewController : UIViewController
  
  @property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
  
  @end
  复制代码
  ViewController.m设置实现属性
  @synthesize scrollView = _scrollView;
  storyboard象库拖拽Viewcontroller并且设置类Viewcontroller
  Image(4)
  点击tableviewCell crl+点击鼠标左键向新Viewcontroller拖拽并且弹storyboard segues并且选择push效
  象库拖拽UIScrollViewViewcontroller并且填充
  Image(5)
  UIScrollView输口连设置Viewcontroller作UIScrollView代理图:
  Image(6)
  现Viewcontroller.m延展添加属性些属性私
  添加
  @synthesize imageView = _imageView;
  现我始设置我UIScrollViewviewDidLoadviewVillAppear
  用面代码:
  复制代码
  - (void)viewDidLoad {
  [super viewDidLoad];
  
  // 1
  UIImage *image = [UIImage imageNamed:@"photo1.png"];
  self.imageView = [[UIImageView alloc] initWithImage:image];
  self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
  [self.scrollView addSubview:self.imageView];
  
  // 2
  self.scrollView.contentSize = image.size;
  
  // 3
  UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
  doubleTapRecognizer.numberOfTapsRequired = 2;
  doubleTapRecognizer.numberOfTouchesRequired = 1;
  [self.scrollView addGestureRecognizer:doubleTapRecognizer];
  
  UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)];
  twoFingerTapRecognizer.numberOfTapsRequired = 1;
  twoFingerTapRecognizer.numberOfTouchesRequired = 2;
  [self.scrollView addGestureRecognizer:twoFingerTapRecognizer];
  }
  
  - (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  
  // 4
  CGRect scrollViewFrame = self.scrollView.frame;
  CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
  CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
  CGFloat minScale = MIN(scaleWidth, scaleHeight);
  self.scrollView.minimumZoomScale = minScale;
  
  // 5
  self.scrollView.maximumZoomScale = 1.0f;
  self.scrollView.zoomScale = minScale;
  
  // 6
  [self centerScrollViewContents];
  }
  复制代码
  面代码看起点复杂我停步步析
  1:第步需要创建UIImageView设置Image属性设置UIImageView坐标并且添加UIScrollView
  2:我设置UIScrollViewcontentSize做目让UIScrollView知道自能向横向竖向滚远或者说少像素
  3:向UIScrollView面添加两手势:双击手势缩另两手指单击放
  4:接我需要计算UIScrollView缩放比例缩放比例1意味着UIScrollView内容展示于1展示内容放
  于1说明内容缩缩放比例需要计算缩放少才能让图片舒适展示UIScrollView根据宽度根据高度做相同计算比较两缩放比例设置UIScrollView缩放比例给缩放比例看整张图片放候
  5:设置缩放比例1缩放比图片辨率看图片模糊设置初始缩放缩放比例图片始充放
  6:让图片永远UIScrollView间缩放候
  复制代码
  - (void)centerScrollViewContents {
  CGSize boundsSize = self.scrollView.bounds.size;
  CGRect contentsFrame = self.imageView.frame;
  
  if (contentsFrame.size.width < boundsSize.width) {
  contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
  } else {
  contentsFrame.origin.x = 0.0f;
  }
  
  if (contentsFrame.size.height < boundsSize.height) {
  contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
  } else {
  contentsFrame.origin.y = 0.0f;
  }
  
  self.imageView.frame = contentsFrame;
  }UIScrollViewbounds于UIImageView图片frame图片坐标条件真计算结相反原始坐标- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
  // 1
  CGPoint pointInView = [recognizer locationInView:self.imageView];
  
  // 2
  CGFloat newZoomScale = self.scrollView.zoomScale * 1.5f;
  newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale);
  
  // 3
  CGSize scrollViewSize = self.scrollView.bounds.size;
  
  CGFloat w = scrollViewSize.width / newZoomScale;
  CGFloat h = scrollViewSize.height / newZoomScale;
  CGFloat x = pointInView.x - (w / 2.0f);
  CGFloat y = pointInView.y - (h / 2.0f);
  
  CGRect rectToZoomTo = CGRectMake(x, y, w, h);
  
  // 4
  [self.scrollView zoomToRect:rectToZoomTo animated:YES];
  }
  复制代码
  1:获点击图片坐标位置.
  2:接计算缩放比例缩放150%必须限制缩放比例
  3:用第步计算位置计算想要缩放位置
  4:需要告诉UIScrollView缩放frame并且加画
  - (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer {
  // Zoom out slightly, capping at the minimum zoom scale specified by the scroll view
  CGFloat newZoomScale = self.scrollView.zoomScale / 1.5f;
  newZoomScale = MAX(newZoomScale, self.scrollView.minimumZoomScale);
  [self.scrollView setZoomScale:newZoomScale animated:YES];
  }
  类似放式
  - (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  // Return the view that you want to zoom
  return self.imageView;
  }
  UIScrollView缩放机制灵魂UIScrollView完缩放候告诉哪视图UIScrollView面实现缩放

  - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  // The scroll view has zoomed, so you need to re-center the contents
  [self centerScrollViewContents];
  }
  UIScrollView完缩放候需要通知视图UIScrollView间否则UIScrollView缩放自
  Image(7)
  编译运行项目现图效试着放缩滚
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式