TileViewController.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #import "TileViewController.h"
  2. #import "SkimViewController.h"
  3. #import "UIImageView+WebCache.h"
  4. @interface TileViewController ()
  5. {
  6. NSArray *_imageNames;
  7. UIScrollView *_scrollView;
  8. }
  9. @end
  10. @implementation TileViewController
  11. - (instancetype)initWithImageNames:(NSArray *)imageNames
  12. {
  13. if (self = [super init]) {
  14. _imageNames = [[NSArray alloc] initWithArray:imageNames];
  15. }
  16. return self;
  17. }
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. //创建滚动视图
  21. [self createScrollView];
  22. //创建图片视图
  23. [self createImageViews];
  24. }
  25. - (void)didReceiveMemoryWarning {
  26. [super didReceiveMemoryWarning];
  27. // Dispose of any resources that can be recreated.
  28. }
  29. - (void)createScrollView
  30. {
  31. //取消对滚动视图内容的布局的影响
  32. if (@available(iOS 11.0, *)) {
  33. _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  34. }else {
  35. self.automaticallyAdjustsScrollViewInsets = NO;
  36. }
  37. CGSize size = self.view.frame.size;
  38. _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height-kNavOffSet-40-50)];
  39. [self.view addSubview:_scrollView];
  40. }
  41. - (void)createImageViews
  42. {
  43. CGSize size = _scrollView.frame.size;
  44. CGFloat gap = 3;
  45. CGFloat width = size.width/4-gap;
  46. CGFloat height = width;//*3/2
  47. for (NSInteger i=0; i<_imageNames.count; i++) {
  48. UIImageView *imageView = [[UIImageView alloc] init];
  49. [imageView sd_setImageWithURL:[NSURL URLWithString:_imageNames[i]] placeholderImage:[UIImage imageNamed:@"暂无图片"]];
  50. imageView.frame = CGRectMake(gap/2+i%4*(width+gap), gap+i/4*(height+gap), width, height);
  51. [_scrollView addSubview:imageView];
  52. _scrollView.contentSize = CGSizeMake(size.width, gap+(_imageNames.count+3)/4*(gap+height));
  53. //添加手势
  54. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
  55. [imageView addGestureRecognizer:tap];
  56. //使用tag值记录图片数组中的下标
  57. imageView.tag = i;
  58. //打开交互
  59. imageView.userInteractionEnabled = YES;
  60. }
  61. }
  62. - (BOOL)prefersStatusBarHidden
  63. {
  64. return self.navigationController.navigationBarHidden;
  65. }
  66. - (void)tapHandle:(UITapGestureRecognizer *)tap
  67. {
  68. SkimViewController *svc = [[SkimViewController alloc] initWithImageNames:_imageNames index:tap.view.tag];
  69. [self.navigationController pushViewController:svc animated:NO];
  70. //告诉应用程序状态栏需要刷新(9.0以后不用管)
  71. [self setNeedsStatusBarAppearanceUpdate];
  72. }
  73. @end