UIView+Shake.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // UIView+Shake.h
  3. // Animations
  4. //
  5. // Created by YouXianMing on 16/2/25.
  6. // Copyright © 2016年 YouXianMing. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. // https://github.com/rFlex/SCViewShaker
  10. #define kDefaultShakeOptions (SCShakeOptionsDirectionHorizontal | SCShakeOptionsForceInterpolationExpDown | SCShakeOptionsAtEndComplete)
  11. #define kDefaultShakeForce (0.075)
  12. #define kDefaultShakeDuration (0.5)
  13. #define kDefaultShakeIterationDuration (0.03)
  14. typedef NS_ENUM(NSUInteger,SCShakeOptions){
  15. SCShakeOptionsDirectionRotate = 0,
  16. SCShakeOptionsDirectionHorizontal = 1,
  17. SCShakeOptionsDirectionVertical = 2,
  18. SCShakeOptionsDirectionHorizontalAndVertical = 3,
  19. SCShakeOptionsForceInterpolationNone = 4,
  20. SCShakeOptionsForceInterpolationLinearUp = 8,
  21. SCShakeOptionsForceInterpolationLinearDown = 16,
  22. SCShakeOptionsForceInterpolationExpUp = 32,
  23. SCShakeOptionsForceInterpolationExpDown = 64,
  24. SCShakeOptionsForceInterpolationRandom = 128,
  25. SCShakeOptionsAtEndRestart = 256,
  26. SCShakeOptionsAtEndComplete = 512,
  27. SCShakeOptionsAtEndContinue = 1024,
  28. SCShakeOptionsAutoreverse = 2048
  29. } ;
  30. typedef void(^ShakeCompletionHandler)();
  31. @interface UIView (Shake)
  32. /**
  33. * Returns whether the view is currently shaking or not.
  34. */
  35. @property (readonly, nonatomic) BOOL isShaking;
  36. /**
  37. * Shake the view using the default options. The view will be shaken for a short amount of time.
  38. */
  39. - (void)shake;
  40. /*
  41. Shake the view using the specified options.
  42. |shakeOptions| is an enum of options that can be activated by using the OR operator (like SCShakeOptionsDirectionRotate | SCShakeOptionsForceInterpolationNone).
  43. |force| is the coefficient of force to apply on each shake iteration (typically between 0 and 1 even though nothing prevents you for setting a higher value if you want).
  44. |duration| is the total duration of the shaking motion. This may be ignored depending of the options you set.
  45. iterationDuration is how long each shake iteration will last. You may want to set a very low value (like 0.02) if you want a proper shake effect.
  46. |completionHandler|, if not null, is the block that will be invoked when the shake finishes.
  47. */
  48. - (void)shakeWithOptions:(SCShakeOptions)shakeOptions
  49. force:(CGFloat)force duration:(CGFloat)duration
  50. iterationDuration:(CGFloat)iterationDuration
  51. completionHandler:(ShakeCompletionHandler)completionHandler;
  52. /**
  53. * End the current shaking action, if any
  54. */
  55. - (void)endShake;
  56. @end