YYImageExampleHelper.m 2.6 KB

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