实现图片的移动和缩放的功能类(move and zoom)
2013-01-09 09:22
465 查看
头文件
--------------------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------------------
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
相关文章推荐
- 实现图片的移动和缩放的功能类(move and zoom)
- Imageview 实现图片移动 缩放功能
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
- PHP图片处理类实现缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色等功能
- Java工具类——实现gif图片缩放与剪切功能
- zoom插件实现图片放大缩小功能
- 图片展现、缩放、移动及裁减的实现
- Android实现图片的缩放翻转功能
- iOS-UI篇—UIScrollView控件实现图片缩放功能
- ImageView里面的图片实现同时移动缩放旋转 photoView自定义
- php实现图片缩放功能类
- [ios2] UIImageView实现图片的移动和缩放 【转】
- 【IOS】扩展UIImageViewEx实现:手势移动,旋转,缩放(附带一个收缩的文字标签功能)
- [Android] 触屏setOnTouchListener实现图片缩放、移动、绘制和添加水印
- 在WPF里面实现以鼠标位置为中心缩放移动图片
- 使用Martix来实现缩放图片的功能
- UIImageView实现图片移动,缩放、旋转的代码片段
- UIImageView实现图片的移动和缩放
- ios学习--结合UIImageView实现图片的移动和缩放
- 结合UIImageView实现图片的移动和缩放