实现图片的移动和缩放的功能类(move and zoom)
来源:互联网 发布:大数据 破获 绑架案 编辑:程序博客网 时间:2024/05/19 00:16
头文件
--------------------------------------------------------------------------------------------
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)
- UIImageView实现图片的移动和缩放
- 图片的移动和缩放
- 图片的缩放和移动
- 图片的缩放功能实现
- 实现ZOOM的功能啊
- svg图片动态缩放功能(jquery-svg-pan-zoom)
- 结合UIImageView实现图片的移动和缩放
- 结合UIImageView实现图片的移动和缩放
- 结合UIImageView实现图片的移动和缩放
- 结合UIImageView实现图片的移动和缩放
- 结合UIImageView实现图片的移动和缩放
- 结合UIImageView实现图片的移动和缩放
- IOS 结合UIImageView实现图片的移动和缩放
- UIImageView实现图片的移动和缩放 【转】
- 结合UIImageView实现图片的移动和缩放
- [ios2] UIImageView实现图片的移动和缩放 【转】
- iOS 之UIImageView实现图片的移动和缩放
- 实现图片抛物线的效果
- 硬盘BPB 格式
- Linux C++的一个时间打印工具类
- D3DXPLANE平面的构造
- 分离删除附加数据库
- 实现图片的移动和缩放的功能类(move and zoom)
- MyEclipse下用XFire开发WebService(入门级)(1)
- smoke test
- C#和Fortran的混合编程
- Network Manager 的种种 Bug
- 编译安装MP4Box
- 软件测试的流程
- High color reproduction notebook
- junit 运用