- #import "MyView.h"
- @implementation MyView
- {
- BOOL pinchZoom;
- CGFloat previousDistance;
- CGFloat zoomFactor;
- }
- -(id)init
- {
- self = [super init];
- if (self) {
- pinchZoom = NO;
- // 缩放前两个触摸点间的距离
- previousDistance =0.0f;
- zoomFactor =1.0f;
- }
- return self;
- }
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- if(event.allTouches.count ==2) {
- pinchZoom = YES;
- NSArray *touches = [event.allTouches allObjects];
- // 接收两个手指的触摸操作
- CGPoint pointOne = [[touches objectAtIndex:0] locationInView:self];
- CGPoint pointTwo = [[touches objectAtIndex:1] locationInView:self];
- // 计算出缩放前后两个手指间的距离绝对值 (勾股定理)
- previousDistance = sqrt(pow(pointOne.x - pointTwo.x,2.0f) +31pow(pointOne.y - pointTwo.y,2.0f));
- }else {
- pinchZoom = NO;
- }
- }
- -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- {
- if(YES == pinchZoom &&event.allTouches.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) +45pow(pointOne.y - pointTwo.y,2.0f));
- // 换算出缩放比例
- zoomFactor += (distance - previousDistance) / previousDistance;
- zoomFactor = fabs(zoomFactor);
- previousDistance = distance;
- // 缩放
- self.layer.transform = CATransform3DMakeScale(zoomFactor, zoomFactor,1.0f);
- }
- }
- -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- {
- if(event.allTouches.count !=2) {
- pinchZoom = NO;
- previousDistance =0.0f;
- }
- }
- -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- {
- }
- @end
来源: