TouchableView.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import "TouchableView.h"
  10. @implementation TouchableView
  11. - (instancetype)initWithFrame:(CGRect)frame
  12. {
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. self.multipleTouchEnabled = YES;
  16. self.numberOFTaps = 0;
  17. self.touchViews = [[NSMutableDictionary alloc] init];
  18. }
  19. return self;
  20. }
  21. - (instancetype)initWithCoder:(NSCoder *)coder
  22. {
  23. self = [super initWithCoder:coder];
  24. if (self) {
  25. self.multipleTouchEnabled = YES;
  26. self.numberOFTaps = 0;
  27. self.touchViews = [[NSMutableDictionary alloc] init];
  28. }
  29. return self;
  30. }
  31. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  32. {
  33. self.numberOFTaps += 1;
  34. [self.delegate shouldHandleTouchesNumber:(int)touches.count];
  35. for (UITouch *touch in touches)
  36. {
  37. [self createViewForTouch:touch];
  38. }
  39. }
  40. - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  41. {
  42. for (UITouch *touch in touches)
  43. {
  44. TouchSpotView *view = [self viewForTouch:touch];
  45. CGPoint newLocation = [touch locationInView:self];
  46. view.center = newLocation;
  47. }
  48. }
  49. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  50. {
  51. for (UITouch *touch in touches)
  52. {
  53. [self removeViewForTouch:touch];
  54. }
  55. [self.delegate shouldHandleTapsNumber:self.numberOFTaps];
  56. }
  57. - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  58. {
  59. for (UITouch *touch in touches)
  60. {
  61. [self removeViewForTouch:touch];
  62. }
  63. }
  64. - (void)createViewForTouch:(UITouch *)touch
  65. {
  66. if (touch)
  67. {
  68. TouchSpotView *newView = [[TouchSpotView alloc] init];
  69. newView.bounds = CGRectMake(0, 0, 1, 1);
  70. newView.center = [touch locationInView:self];
  71. [self addSubview:newView];
  72. [UIView animateWithDuration:0.2 animations:^{
  73. newView.bounds = CGRectMake(0, 0, 100, 100);
  74. }];
  75. self.touchViews[[self touchHash:touch]] = newView;
  76. }
  77. }
  78. - (TouchSpotView *)viewForTouch:(UITouch *)touch
  79. {
  80. return self.touchViews[[self touchHash:touch]];
  81. }
  82. - (void)removeViewForTouch:(UITouch *)touch
  83. {
  84. NSNumber *touchHash = [self touchHash:touch];
  85. UIView *view = self.touchViews[touchHash];
  86. if (view)
  87. {
  88. [view removeFromSuperview];
  89. [self.touchViews removeObjectForKey:touchHash];
  90. }
  91. }
  92. - (NSNumber *)touchHash:(UITouch *)touch
  93. {
  94. return [NSNumber numberWithUnsignedInteger:touch.hash];
  95. }
  96. @end