YYImageExampleHelper.m 2.8 KB

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