JXImageView.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // JXImageView.m
  3. // textScr
  4. //
  5. // Created by JK PENG on 11-8-17.
  6. // Copyright 2011年 Devdiv. All rights reserved.
  7. //
  8. #import "JXImageView.h"
  9. //遵循协议--
  10. @interface JXImageView () <UIGestureRecognizerDelegate, CAAnimationDelegate>
  11. @property (nonatomic, assign) BOOL isAction; //防止重复点击
  12. @end
  13. @implementation JXImageView
  14. @synthesize delegate;
  15. @synthesize didTouch;
  16. @synthesize changeAlpha;
  17. @synthesize animationType;
  18. @synthesize selected;
  19. @synthesize enabled;
  20. - (id)init
  21. {
  22. self = [super init];
  23. if (self) {
  24. [self doSet];
  25. }
  26. return self;
  27. }
  28. - (id)initWithFrame:(CGRect)frame
  29. {
  30. self = [super initWithFrame:frame];
  31. if (self) {
  32. [self doSet];
  33. }
  34. return self;
  35. }
  36. -(void)doSet{
  37. _canChange = NO;
  38. selected = NO;
  39. enabled = NO;
  40. _isAction = NO;
  41. }
  42. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  43. // NSLog(@"touchesBegan");
  44. //[super touchesBegan: touches withEvent: event];
  45. if(_canChange && changeAlpha)
  46. self.alpha = 0.5;
  47. }
  48. -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
  49. // NSLog(@"touchesMoved");
  50. [super touchesMoved: touches withEvent: event];
  51. if ([self.panDelegate respondsToSelector:@selector(getTouchWhenMove:withTouch:withEvent:withLongPressGes:)]) {
  52. [self.panDelegate getTouchWhenMove:self withTouch:touches withEvent:event withLongPressGes:self.longPress];
  53. }
  54. }
  55. -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
  56. NSLog(@"touchesEnded");
  57. [super touchesEnded: touches withEvent: event];
  58. if(_canChange)
  59. self.alpha = 1;
  60. if (_isAction) {
  61. return;
  62. }
  63. self.isAction = YES;
  64. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  65. self.isAction = NO;
  66. });
  67. BOOL inside = YES;
  68. for(int i=0;i<[touches count];i++){
  69. CGPoint p = [[[touches allObjects] objectAtIndex:i] locationInView:self];
  70. NSLog(@"%d=%f,%f",i,p.x,p.y);
  71. if(p.x<0 || p.y <0){
  72. inside = NO;
  73. break;
  74. }
  75. if(p.x>self.frame.size.width || p.y>self.frame.size.height){
  76. inside = NO;
  77. break;
  78. }
  79. }
  80. if(!inside){
  81. if(self.delegate != nil && [self.delegate respondsToSelector:self.didDragout])
  82. [self.delegate performSelectorOnMainThread:self.didDragout withObject:self waitUntilDone:NO];
  83. return;
  84. }
  85. if(self.delegate != nil && [self.delegate respondsToSelector:self.didTouch]){
  86. [self.delegate performSelectorOnMainThread:self.didTouch withObject:self waitUntilDone:NO];
  87. }
  88. }
  89. -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
  90. // [super touchesCancelled: touches withEvent: event];
  91. // NSLog(@"touchesCancelled");
  92. if(_canChange)
  93. self.alpha = 1;
  94. for(int i=0;i<[touches count];i++){
  95. [[[touches allObjects] objectAtIndex:i] locationInView:self];
  96. // NSLog(@"%d=%f,%f",i,p.x,p.y);
  97. }
  98. }
  99. - (void)dealloc
  100. {
  101. delegate = nil;
  102. didTouch = nil;
  103. // [super dealloc];
  104. }
  105. -(void)setImage:(UIImage *)image{
  106. switch (self.animationType) {
  107. case JXImageView_Animation_More:
  108. [self addAnimationPage:2];
  109. break;
  110. case JXImageView_Animation_Line:
  111. [self addAnimation:jx_showImage_time];
  112. break;
  113. default:
  114. break;
  115. }
  116. [super setImage:image];
  117. }
  118. -(void)addAnimation:(int)nTime
  119. {
  120. CATransition *transition = [CATransition animation];
  121. // Animate over 3/4 of a second
  122. transition.duration = nTime;
  123. // using the ease in/out timing function
  124. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  125. transition.type = kCATransitionFade;
  126. transition.delegate = self;
  127. [self.layer addAnimation:transition forKey:nil];
  128. }
  129. -(void)addAnimationPage:(int)nTime{
  130. // First create a CATransition object to describe the transition
  131. CATransition *transition = [CATransition animation];
  132. // Animate over 3/4 of a second
  133. transition.duration = nTime;
  134. // using the ease in/out timing function
  135. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  136. NSString *types[4] = {kCATransitionMoveIn, kCATransitionPush, kCATransitionReveal, kCATransitionFade};
  137. NSString *subtypes[4] = {kCATransitionFromLeft, kCATransitionFromRight};
  138. int rnd = random() % 4;
  139. transition.type = types[rnd];
  140. if(rnd < 3) // if we didn't pick the fade transition, then we need to set a subtype too
  141. {
  142. transition.subtype = subtypes[random() % 2];
  143. }
  144. transition.delegate = self;
  145. [self.layer addAnimation:transition forKey:nil];
  146. }
  147. -(void)setDidTouch:(SEL)value{
  148. if(value){
  149. didTouch = value;
  150. _canChange = YES;
  151. self.userInteractionEnabled = YES;
  152. changeAlpha = YES;
  153. }
  154. }
  155. - (void)addTapGesture{
  156. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSave:)];
  157. [tap requireGestureRecognizerToFail:self.longPress];
  158. [self addGestureRecognizer:tap];
  159. }
  160. - (void)addLongPressGesture{
  161. self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressSave:)];
  162. self.longPress.cancelsTouchesInView = NO;
  163. [self addGestureRecognizer:self.longPress];
  164. }
  165. - (void)tapSave:(UITapGestureRecognizer *)tap{
  166. if ([self.panDelegate respondsToSelector:@selector(tapImageView:)]) {
  167. [self.panDelegate tapImageView:self];
  168. }
  169. }
  170. - (void)longPressSave:(UILongPressGestureRecognizer *)gesture{
  171. self.highlighted = YES;
  172. if ([self.panDelegate respondsToSelector:@selector(changeWhenPan: gesture:)]) {
  173. [self.panDelegate changeWhenPan:self gesture:gesture];
  174. }
  175. }
  176. @end