自定义一个可拖拽、旋转、缩放的View

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值