YYImageExampleHelper.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // YYImageExampleUtils.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/7/20.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "YYImageExampleHelper.h"
  9. #import "YYImage.h"
  10. #import "UIView+YYAdd.h"
  11. #import "CALayer+YYAdd.h"
  12. #import "UIGestureRecognizer+YYAdd.h"
  13. #import <ImageIO/ImageIO.h>
  14. #import <Accelerate/Accelerate.h>
  15. //#import <bpg/libbpg.h>
  16. @implementation YYImageExampleHelper
  17. + (void)addTapControlToAnimatedImageView:(YYAnimatedImageView *)view {
  18. if (!view) return;
  19. view.userInteractionEnabled = YES;
  20. __weak typeof(view) _view = view;
  21. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
  22. if ([_view isAnimating]) [_view stopAnimating];
  23. else [_view startAnimating];
  24. // add a "bounce" animation
  25. UIViewAnimationOptions op = UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionBeginFromCurrentState;
  26. [UIView animateWithDuration:0.1 delay:0 options:op animations:^{
  27. _view.layer.transformScale = 0.97;
  28. } completion:^(BOOL finished) {
  29. [UIView animateWithDuration:0.1 delay:0 options:op animations:^{
  30. _view.layer.transformScale = 1.008;
  31. } completion:^(BOOL finished) {
  32. [UIView animateWithDuration:0.1 delay:0 options:op animations:^{
  33. _view.layer.transformScale = 1;
  34. } completion:NULL];
  35. }];
  36. }];
  37. }];
  38. [view addGestureRecognizer:tap];
  39. }
  40. + (void)addPanControlToAnimatedImageView:(YYAnimatedImageView *)view {
  41. if (!view) return;
  42. view.userInteractionEnabled = YES;
  43. __weak typeof(view) _view = view;
  44. __block BOOL previousIsPlaying;
  45. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithActionBlock:^(id sender) {
  46. UIImage<YYAnimatedImage> *image = (id)_view.image;
  47. if (![image conformsToProtocol:@protocol(YYAnimatedImage)]) return;
  48. UIPanGestureRecognizer *gesture = sender;
  49. CGPoint p = [gesture locationInView:gesture.view];
  50. CGFloat progress = p.x / gesture.view.width;
  51. if (gesture.state == UIGestureRecognizerStateBegan) {
  52. previousIsPlaying = [_view isAnimating];
  53. [_view stopAnimating];
  54. _view.currentAnimatedImageIndex = image.animatedImageFrameCount * progress;
  55. } else if (gesture.state == UIGestureRecognizerStateEnded ||
  56. gesture.state == UIGestureRecognizerStateCancelled) {
  57. if (previousIsPlaying) [_view startAnimating];
  58. } else {
  59. _view.currentAnimatedImageIndex = image.animatedImageFrameCount * progress;
  60. }
  61. }];
  62. [view addGestureRecognizer:pan];
  63. }
  64. @end