#import <UIKit/UIKit.h>
@interface TouchView : UIView
@end
#import "TouchView.h"
@implementation TouchView
#pragma mark - 计算两点之间的距离
- (CGFloat)distance:(CGPoint)point1 point2:(CGPoint)point2
{
CGFloat dx = point1.x - point2.x;
CGFloat dy = point1.y - point2.y;
CGFloat tance = sqrt(pow(dx, 2) + pow(dy, 2));
return tance;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
//设置是否支持多点触摸(YES支持,NO不支持)
self.multipleTouchEnabled = YES;
}
return self;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
if (1 == [touches count])
{
// 拿到UITouch就能获取当前点
UITouch *touch = [touches anyObject];
// 获取当前点
CGPoint curP = [touch locationInView:self.superview];
// 获取上一个点
CGPoint preP = [touch previousLocationInView:self.superview];
// 获取手指x轴偏移量
CGFloat offsetX = curP.x - preP.x;
// 获取手指y轴偏移量
CGFloat offsetY = curP.y - preP.y;
// 移动当前view
self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
}else if (2 == [touches count])
{
NSArray *array = [touches allObjects];
UITouch *touch1 = [array firstObject];
UITouch *touch2 = [array lastObject];
// 获取移动前的触摸点
CGPoint firstPreviousPoint = [touch1 previousLocationInView:self.superview];
CGPoint secondPreviousPoint = [touch2 previousLocationInView:self.superview];
// 获取移动后的触摸点
CGPoint firstCurrentPoint = [touch1 locationInView:self.superview];
CGPoint secondCurrentPoint = [touch2 locationInView:self.superview];
// 旋转
[self rotateTouchViewFirstPreviousPoint:firstPreviousPoint
secondPreviousPoint:secondPreviousPoint
firstCurrentPoint:firstCurrentPoint
secondCurrentPoint:secondCurrentPoint];
// 缩放
[self scaleTouchViewFirstPreviousPoint:firstPreviousPoint
secondPreviousPoint:secondPreviousPoint
firstCurrentPoint:firstCurrentPoint
secondCurrentPoint:secondCurrentPoint];
}
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
}
/***********************分割线*****************************/
/************************旋转******************************/
- (void)rotateTouchViewFirstPreviousPoint:(CGPoint)firstPreviousPoint
secondPreviousPoint:(CGPoint)secondPreviousPoint
firstCurrentPoint:(CGPoint)firstCurrentPoint
secondCurrentPoint:(CGPoint)secondCurrentPoint
{
// 获取移动前两触摸点所在线段 与 移动后两触摸点所在线段 的交点
CGPoint intersection = [self intersectionFirstPreviousPoint:firstPreviousPoint secondPreviousPoint:secondPreviousPoint firstCurrentPoint:firstCurrentPoint secondCurrentPoint:secondCurrentPoint];
// 计算第一个触摸点(移动前)到交点的距离
CGFloat firstDistance = [self distance:intersection point2:firstPreviousPoint];
// 计算第二个触摸点(移动前)到交点的距离
CGFloat secondDistance = [self distance:intersection point2:secondPreviousPoint];
// 获取旋转中心点
CGPoint rotatingCenter;
float previousAngle, currentAngle;
if (firstDistance <= secondDistance)
{
// 第一触摸点 与 旋转中心更加靠近
rotatingCenter.x = firstPreviousPoint.x + (firstCurrentPoint.x - firstPreviousPoint.x) / 2;
rotatingCenter.y = firstPreviousPoint.y + (firstCurrentPoint.y - firstPreviousPoint.y) / 2;
// rotatingCenter = firstPreviousPoint;
previousAngle = [self getTouchAngle:secondPreviousPoint
rotatingCenter:firstPreviousPoint];
currentAngle = [self getTouchAngle:secondCurrentPoint
rotatingCenter:firstCurrentPoint];
}else
{
// 第二触摸点 与 旋转中心更加靠近
rotatingCenter.x = secondPreviousPoint.x + (secondCurrentPoint.x - secondPreviousPoint.x) / 2;
rotatingCenter.y = secondPreviousPoint.y + (secondCurrentPoint.y - secondPreviousPoint.y) / 2;
// rotatingCenter = secondPreviousPoint;
previousAngle = [self getTouchAngle:firstPreviousPoint
rotatingCenter:secondPreviousPoint];
currentAngle = [self getTouchAngle:firstCurrentPoint
rotatingCenter:secondCurrentPoint];
}
// 获取旋转角度
CGFloat direction = currentAngle - previousAngle;
NSLog(@"direction = %lf", direction);
if (direction < 0.002 && direction > -0.002)
{
return;
}
// 更新锚点
CGFloat current_W = self.bounds.size.width;
CGFloat current_H = self.bounds.size.height;
CGPoint tempPoint = [self convertPoint:rotatingCenter fromView:self.superview];
CGPoint newAnchorPoint = CGPointMake(tempPoint.x / current_W, tempPoint.y / current_H);
self.layer.anchorPoint = newAnchorPoint;
self.layer.shouldRasterize = YES;
// 旋转
self.transform = CGAffineTransformRotate(self.transform, direction);
// 恢复默认锚点
self.layer.anchorPoint = CGPointMake(0.5, 0.5);
}
#pragma mark - 求两条相交线段的交点
- (CGPoint)intersectionFirstPreviousPoint:(CGPoint)firstPreviousPoint
secondPreviousPoint:(CGPoint)secondPreviousPoint
firstCurrentPoint:(CGPoint)firstCurrentPoint
secondCurrentPoint:(CGPoint)secondCurrentPoint
{
CGPoint intersection;
CGFloat X1 = (firstCurrentPoint.x - firstCurrentPoint.x) * (secondPreviousPoint.y - firstPreviousPoint.y);
CGFloat X2 = (secondPreviousPoint.x - firstPreviousPoint.x) * (secondCurrentPoint.y - firstCurrentPoint.y);
intersection.y = (firstCurrentPoint.y * X1 - firstPreviousPoint.y * X2) / (X1 - X2);
intersection.x = firstPreviousPoint.x + (intersection.y - firstPreviousPoint.y) * (secondPreviousPoint.x - firstPreviousPoint.x) / (secondPreviousPoint.y - firstPreviousPoint.y);
return intersection;
}
#pragma mark - 求线段 相对于 X轴的夹角
- (float)getTouchAngle:(CGPoint)touch rotatingCenter:(CGPoint)rotatingCenter
{
// Translate into cartesian space with origin at the center of a 320-pixel square
float x = touch.x - rotatingCenter.x;
float y = -(touch.y - rotatingCenter.y);
// Take care not to divide by zero!
if (y == 0)
{
if (x > 0)
{
return M_PI_2;
}
else {
return 3 * M_PI_2;
}
}
float arctan = atanf(x/y);
// Figure out which quadrant we're in
// Quadrant I
if ((x >= 0) && (y > 0))
{
return arctan;
}
// Quadrant II
else if ((x < 0) && (y > 0))
{
return arctan + 2 * M_PI;
}
// Quadrant III
else if ((x <= 0) && (y < 0))
{
return arctan + M_PI;
}
// Quadrant IV
else if ((x > 0) && (y < 0)) {
return arctan + M_PI;
}
return -1;
}
/***********************分割线*****************************/
/************************缩放******************************/
#pragma mark - 缩放
- (void)scaleTouchViewFirstPreviousPoint:(CGPoint)firstPreviousPoint
secondPreviousPoint:(CGPoint)secondPreviousPoint
firstCurrentPoint:(CGPoint)firstCurrentPoint
secondCurrentPoint:(CGPoint)secondCurrentPoint
{
//获取移动前的两点之间的距离
CGFloat previousDistance = [self distance:firstPreviousPoint point2:secondPreviousPoint];
//获取移动后的两点之间的距离
CGFloat currentDistance = [self distance:firstCurrentPoint point2:secondCurrentPoint];
// 更新锚点
CGFloat current_W = self.bounds.size.width;
CGFloat current_H = self.bounds.size.height;
CGPoint tempPoint = [self convertPoint:self.superview.center fromView:self.superview.superview];
CGPoint newAnchorPoint = CGPointMake(tempPoint.x / current_W, tempPoint.y / current_H);
self.layer.anchorPoint = newAnchorPoint;
self.layer.shouldRasterize = YES;
//获取缩放比例
CGFloat scale = currentDistance / previousDistance;
// 缩放视图
self.bounds = CGRectMake(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale);
// 子视图缩放处理
[self subViews:self scale:scale];
// 恢复默认锚点
self.layer.anchorPoint = CGPointMake(0.5, 0.5);
}
#pragma mark - 子视图缩放处理
- (void)subViews:(UIView *)view scale:(CGFloat)scale
{
NSArray *subviews = view.subviews;
if (subviews.count > 0)
{
for (UIView *subView in subviews)
{
CGRect subFrame = CGRectMake(subView.frame.origin.x * scale, subView.frame.origin.y * scale, subView.frame.size.width * scale, subView.frame.size.height * scale);
[subView setFrame:subFrame];
[self subViews:subView scale:scale];
}
}
}
@end
转载于:https://www.cnblogs.com/cnjacob/p/6698207.html