ViewController.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // ViewController.m
  3. //
  4. // Copyright (c) 2015-2016 Evgeny Aleksandrov. License: MIT.
  5. #import "ViewController.h"
  6. #import <EARestrictedScrollView/EARestrictedScrollView.h>
  7. @interface ViewController () {
  8. EARestrictedScrollView *restrictedScrollView;
  9. }
  10. @end
  11. @implementation ViewController
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14. [self.view setBackgroundColor:[UIColor blackColor]];
  15. restrictedScrollView = [[EARestrictedScrollView alloc] initWithFrame:self.view.frame];
  16. restrictedScrollView.alwaysBounceHorizontal = YES;
  17. restrictedScrollView.alwaysBounceVertical = YES;
  18. [self.view addSubview:restrictedScrollView];
  19. UIImage *bgImage = [UIImage imageNamed:@"milky-way.jpg"];
  20. UIImageView *imageView = [[UIImageView alloc] initWithImage:bgImage];
  21. [restrictedScrollView addSubview:imageView];
  22. [restrictedScrollView setContentSize:imageView.frame.size];
  23. [self addViewWithControlsAtFrame:CGRectMake(20.f, 50.f, self.view.frame.size.width, self.view.frame.size.height)];
  24. [self addViewWithControlsAtFrame:CGRectMake(50.f + self.view.frame.size.width, 50.f, self.view.frame.size.width * 1.5f, self.view.frame.size.height * 1.5f)];
  25. }
  26. - (void)addViewWithControlsAtFrame:(CGRect)viewFrame {
  27. UIView *restrictionArea = [[UIView alloc] initWithFrame:viewFrame];
  28. restrictionArea.layer.cornerRadius = 10.f;
  29. restrictionArea.layer.borderColor = [UIColor whiteColor].CGColor;
  30. restrictionArea.layer.borderWidth = 2.f;
  31. UISwitch *areaSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
  32. [areaSwitch setFrame:CGRectMake((restrictionArea.frame.size.width - areaSwitch.frame.size.width)/2, (restrictionArea.frame.size.height - areaSwitch.frame.size.height)/2, 0, 0)];
  33. [areaSwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
  34. [restrictionArea addSubview:areaSwitch];
  35. [restrictedScrollView addSubview:restrictionArea];
  36. }
  37. - (void)changeSwitch:(id)sender {
  38. UISwitch *areaSwitch = (UISwitch *)sender;
  39. if([areaSwitch isOn]){
  40. [restrictedScrollView setRestrictionArea:areaSwitch.superview.frame];
  41. } else {
  42. [restrictedScrollView setRestrictionArea:CGRectZero];
  43. }
  44. }
  45. @end