头文件
--------------------------------------------------------------------------------------------
EditImgView.h
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
@interface EditImgView : UIImageView {
CGFloat zoom;
CGPoint previousPoint; // used in move
BOOL moving;
CGFloat previousDistance; // used in pinch zoom
}
-(id)initWithImg:(UIImage*)img;
-(void)configureTiledLayer;
@end
--------------------------------------------------------------------------------------------
实现类
---------
EditImgView.m
#import "EditImgView.h"
#import "Logger.h"
#define EXPAND_FRE 5/4
#define REDUCE_FRE 4/5
#define MIDDLE_FRE 1
#define MULTIPLE 3
@implementation EditImgView
-(id)initWithImg:(UIImage*)img
{
if (self = [super init])
{
self.image = img;
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = YES;
[self configureTiledLayer];
[self setNeedsLayout];
}
return self;
}
- (void)configureTiledLayer {
zoom = MIDDLE_FRE;
moving = NO;
}
- (void)setZoom:(CGFloat)newZoom {
zoom = newZoom;
self.transform = CGAffineTransformMakeScale(zoom,zoom);
}
- (void)zoomIn {
[self setZoom:zoom * EXPAND_FRE];
}
- (void)zoomOut {
[self setZoom:zoom * REDUCE_FRE];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
touches = [event allTouches];
if(touches.count >= 2)
{
NSArray *touches = [event.allTouches allObjects];
CGPoint pointOne = [[touches objectAtIndex:0] previousLocationInView:self];
CGPoint pointTwo = [[touches objectAtIndex:1] previousLocationInView:self];
previousDistance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) +
pow(pointOne.y - pointTwo.y, 2.0f));
}
else if(touches.count == 1)
{
previousPoint = [[touches anyObject] locationInView:self];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
if(touches.count == 1) {
CGPoint currentPoint = [[touches anyObject] locationInView:self];
previousPoint = [[touches anyObject] previousLocationInView:self];
if (![NSStringFromCGPoint(currentPoint) isEqualToString:NSStringFromCGPoint(previousPoint)])
{
CGPoint delta = CGPointMake(currentPoint.x - previousPoint.x, currentPoint.y - previousPoint.y);
self.center = CGPointMake(self.center.x + delta.x * zoom,
self.center.y + delta.y * zoom);
previousPoint = currentPoint;
NSLog(@"---------string == %@",NSStringFromCGRect(self.frame));
moving = YES;
}
}
else if (touches.count != 2)
{
return;
}
else if(touches.count == 2) {
NSArray *touches = [event.allTouches allObjects];
CGPoint pointOne = [[touches objectAtIndex:0] locationInView:self];
CGPoint pointTwo = [[touches objectAtIndex:1] locationInView:self];
CGFloat distance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) +
pow(pointOne.y - pointTwo.y, 2.0f));
CGFloat newZoom = 1;
if (previousDistance != 0)
{
newZoom = fabs(zoom+(distance - previousDistance) / (previousDistance));
}
[self setZoom:newZoom];
previousDistance = distance;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(!moving) {
if(touches.count == 1) {
if([[touches anyObject] tapCount] == 2) {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self zoomOut];
} else {
[self performSelector:@selector(zoomIn) withObject:nil afterDelay:0.25];
}
}
} else {
moving = NO;
}
}
- (void)dealloc {
[super dealloc];
}
@end