实现图片的移动和缩放的功能类(move and zoom)
头文件
--------------------------------------------------------------------------------------------
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2012-01-09 ipod, iphone, ipad的区别
2012-01-09 pb中 执行动态sql