#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
typedef enum{
UIImageExNormal = 0,
UIImageExFull
}UIImageExState;
@interface UIImageViewEx : UIImageView<UIGestureRecognizerDelegate>
{
UIView *parentview; // , UIImageEx UIView
BOOL isPanEnable; //
BOOL isPinchEnable; //
BOOL isRotateEnable; //
BOOL isTap; //
UIImageExState imageState; //
CGFloat imageScale; //
CGFloat imageSize; //
CGFloat imageRotation; //
CGPoint imagePoint; //
UITextView *textView; //
}
@property (nonatomic,retain) UIView *parentview;
@property (nonatomic) CGFloat imageSize;
@property (nonatomic) CGFloat imageRotation;
@property (nonatomic) CGPoint imagePoint;
@property BOOL isPanEnable;
@property BOOL isRotateEnable;
@property BOOL isPinchEnable;
@property BOOL isTap;
- (void)handlePan:(UIPanGestureRecognizer *)recognizer;
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer;
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer;
- (void)handleTap:(UITapGestureRecognizer *)recognizer;
//
- (void)setScaleAndRotation:(UIView*)imageView;
- (void)setInfoText:(NSString *)string;
- (void)setShadow:(BOOL)isShadow;
@end
#import "UIImageViewEx.h"
@implementation UIImageViewEx
@synthesize parentview;
@synthesize isRotateEnable,isPanEnable,isPinchEnable,isTap;
@synthesize imageSize,imageRotation,imagePoint;
/*
* SetScaleAndRotation ImageView , ,
* @parent UIView
*/
- (void)setScaleAndRotation:(UIView*) parent
{
parentview=parent;
parentview.userInteractionEnabled=YES;
isPanEnable=YES;
isPinchEnable=YES;
isRotateEnable=YES;
isTap = YES;
imageSize=1;
imageRotation=0;
imageScale= self.parentview.frame.size.width/self.frame.size.width;
imagePoint=self.frame.origin;
self.userInteractionEnabled=YES;
UIPanGestureRecognizer *panRcognize=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panRcognize.delegate=self;
[panRcognize setEnabled:YES];
[panRcognize delaysTouchesEnded];
[panRcognize cancelsTouchesInView];
UIPinchGestureRecognizer *pinchRcognize=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[pinchRcognize setEnabled:YES];
[pinchRcognize delaysTouchesEnded];
[pinchRcognize cancelsTouchesInView];
UIRotationGestureRecognizer *rotationRecognize=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
[rotationRecognize setEnabled:YES];
[rotationRecognize delaysTouchesEnded];
[rotationRecognize cancelsTouchesInView];
rotationRecognize.delegate=self;
pinchRcognize.delegate=self;
UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
tapRecognize.numberOfTapsRequired = 1;
tapRecognize.delegate = self;
[tapRecognize setEnabled :YES];
[tapRecognize delaysTouchesBegan];
[tapRecognize cancelsTouchesInView];
[self addGestureRecognizer:rotationRecognize];
[self addGestureRecognizer:panRcognize];
[self addGestureRecognizer:pinchRcognize];
[self addGestureRecognizer:tapRecognize];
}
/*
* setInfoText
* @string NSString
*/
- (void)setInfoText:(NSString *)string
{
if (textView!=nil) {
[textView removeFromSuperview];
textView = nil;
}
textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 0, 30)];
textView.text = string;
textView.hidden = YES;
textView.backgroundColor = [UIColor blueColor];
textView.textColor =[UIColor whiteColor];
[self addSubview:textView];
}
/*
* SetShadow
* @isShadow BOOL YES ,NO
*/
- (void)setShadow:(BOOL)isShadow
{
if (!isShadow) {
[[self layer] setShadowOffset:CGSizeMake(0, 0)];
[[self layer] setShadowRadius:0];
[[self layer] setShadowOpacity:1];
[[self layer] setShadowColor:[UIColor whiteColor].CGColor];
return;
}
[[self layer] setShadowOffset:CGSizeMake(3, 3)];
[[self layer] setShadowRadius:3];
[[self layer] setShadowOpacity:0.5];
[[self layer] setShadowColor:[UIColor blackColor].CGColor];
}
#pragma UIGestureRecognizer Handles
/*
*
* @recognizer
*/
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
if (!isPanEnable) {
return;
}
[self setShadow:YES];
CGPoint translation = [recognizer translationInView:parentview];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:parentview];
if (recognizer.state == UIGestureRecognizerStateEnded) {
[UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.center=CGPointMake(imagePoint.x+self.frame.size.width/2, imagePoint.y+self.frame.size.height/2);
} completion:nil];
[self setShadow:NO];
}
}
/*
* handPinch
* @recognizer UIPinchGestureRecognizer
*/
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer{
if (!isPinchEnable) {
return;
}
imageSize*=recognizer.scale;
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
if (recognizer.state==UIGestureRecognizerStateEnded) {
[UIView animateWithDuration:.35 animations:^{
if (imageSize >=1 && imageState == UIImageExNormal) {
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform,imageScale/imageSize, imageScale/imageSize);
imageState = UIImageExFull;
}
else if(imageSize<1 && imageState == UIImageExFull)
{
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, 1/(imageScale*imageSize), 1/(imageScale*imageSize));
imageState = UIImageExNormal;
}else {
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, 1/imageSize,1/imageSize);
}
[UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.center=CGPointMake(imagePoint.x+self.frame.size.width/2, imagePoint.y+self.frame.size.height/2);
} completion:nil];
recognizer.scale = 1;
imageSize = 1;
}];
}
recognizer.scale = 1;
}
/*
* handleRotate
* recognizer UIRotationGestureRecognizer
*/
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer{
if (!isRotateEnable) {
return;
}
imageRotation+=recognizer.rotation;
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
if (recognizer.state==UIGestureRecognizerStateEnded) {
[UIView animateWithDuration:.35 animations:^{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, -imageRotation);
recognizer.view.center=CGPointMake(imagePoint.x+self.frame.size.width/2, imagePoint.y+self.frame.size.height/2);
}];
imageRotation=0;
}
recognizer.rotation = 0;
}
/*
* handleTap
* @recognizer UITapGestureRecognizer
*/
-(void) handleTap:(UITapGestureRecognizer *)recognizer
{
if (!isTap) {
return;
}
if (textView.hidden) {
[UIView animateWithDuration:0.35 delay:0.15 options:UIViewAnimationOptionTransitionCurlUp animations:^{
textView.hidden = NO;
textView.frame = CGRectMake(0, 0, 120, 30);
} completion:nil];
}else {
[UIView animateWithDuration:0.35 delay:0.15 options:UIViewAnimationOptionTransitionCurlUp animations:^{
textView.frame = CGRectMake(0, 0, 0, 30);
} completion:^(BOOL finished){
if (finished){
textView.hidden = YES;
}
}];
}
}
#pragma UIGestureRecognizerDelegate
/*
* gestureRecognizer ,
* @return YES ,NO
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
@end