ZFFloatView.m 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // ZFFloatView.m
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "ZFFloatView.h"
  25. @implementation ZFFloatView
  26. - (instancetype)initWithFrame:(CGRect)frame {
  27. self = [super initWithFrame:frame];
  28. if (self) {
  29. [self initilize];
  30. }
  31. return self;
  32. }
  33. - (instancetype)initWithCoder:(NSCoder *)aDecoder{
  34. self = [super initWithCoder:aDecoder];
  35. if (self) {
  36. [self initilize];
  37. }
  38. return self;
  39. }
  40. - (void)initilize {
  41. self.safeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  42. UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(doMoveAction:)];
  43. [self addGestureRecognizer:panGestureRecognizer];
  44. }
  45. - (void)setParentView:(UIView *)parentView {
  46. _parentView = parentView;
  47. [parentView addSubview:self];
  48. }
  49. #pragma mark - Action
  50. - (void)doMoveAction:(UIPanGestureRecognizer *)recognizer {
  51. /// The position where the gesture is moving in the self.view.
  52. CGPoint translation = [recognizer translationInView:self.parentView];
  53. CGPoint newCenter = CGPointMake(recognizer.view.center.x + translation.x,
  54. recognizer.view.center.y + translation.y);
  55. // Limited screen range:
  56. // Top margin limit.
  57. newCenter.y = MAX(recognizer.view.frame.size.height/2 + self.safeInsets.top, newCenter.y);
  58. // Bottom margin limit.
  59. newCenter.y = MIN(self.parentView.frame.size.height - self.safeInsets.bottom - recognizer.view.frame.size.height/2, newCenter.y);
  60. // Left margin limit.
  61. newCenter.x = MAX(recognizer.view.frame.size.width/2, newCenter.x);
  62. // Right margin limit.
  63. newCenter.x = MIN(self.parentView.frame.size.width - recognizer.view.frame.size.width/2,newCenter.x);
  64. // Set the center point.
  65. recognizer.view.center = newCenter;
  66. // Set the gesture coordinates to 0, otherwise it will add up.
  67. [recognizer setTranslation:CGPointZero inView:self.parentView];
  68. }
  69. @end