[iOS]分享一段用UITouch事件来实现View的旋转缩放移动的核心代码

分享一段用UITouch事件来实现View的旋转缩放移动的核心代码

//MARK:- Touch Event
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        self.transform = CGAffineTransformConcat(self.transform, solveTransform(touches, self))
    }
    
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        
    }
    
    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
        self.touchesEnded(touches, withEvent: event)
    }

//MARK:- 计算新的transform
/**
根据 touches 事件 和 事件发送的view 计算 变换矩阵

:param: touches
:param: inView  touch事件的接受view

:returns: 返回变换矩阵
*/
public func solveTransform(touches: NSSet, inView: UIView) -> CGAffineTransform {
    // 单点移动
    if touches.count == 1 {
        let touch = touches.anyObject() as UITouch
        let pointPrevious = touch.previousLocationInView(inView.superview)
        let point = touch.locationInView(inView.superview)
        
        return CGAffineTransformMakeTranslation(point.x - pointPrevious.x, point.y - pointPrevious.y)
    }
    // 双点缩放和旋转
    else if touches.count == 2 {
        let touch1 = touches.allObjects[0] as UITouch
        let touch2 = touches.allObjects[1] as UITouch

        return solveTransform(touch1.previousLocationInView(inView.superview),
            touch1.locationInView(inView.superview),
            touch2.previousLocationInView(inView.superview),
            touch2.locationInView(inView.superview),
            inView.layer.position)
    }
    else {
        return CGAffineTransformIdentity
    }
}


/**
根据 4个点 和 1个坐标原点计算 变换矩阵

:param: point1Previous 之前的点1
:param: point1         移动到点1
:param: point2Previous 之前的点2
:param: point2         移动到点2
:param: center         坐标中心点

:returns: 返回变换矩阵
*/
public func solveTransform(point1Previous: CGPoint, point1: CGPoint, point2Previous: CGPoint, point2: CGPoint, center: CGPoint) -> CGAffineTransform {
    let x1 = point1Previous.x - center.x
    let y1 = point1Previous.y - center.y
    let x2 = point2Previous.x - center.x
    let y2 = point2Previous.y - center.y
    let x3 = point1.x - center.x
    let y3 = point1.y - center.y
    let x4 = point2.x - center.x
    let y4 = point2.y - center.y
    
    // Solve the system:
    //   [a b t1, -b a t2, 0 0 1] * [x1, y1, 1] = [x3, y3, 1]
    //   [a b t1, -b a t2, 0 0 1] * [x2, y2, 1] = [x4, y4, 1]
    
    var transform: CGAffineTransform
    let D = pow((y1 - y2), 2) + pow((x1 - x2), 2)
    if (D < 0.1) {
        return CGAffineTransformMakeTranslation(x3 - x1, y3 - y1)
    }
    else {
        let a = (y1 - y2)*(y3 - y4) + (x1 - x2)*(x3 - x4);
        let b = (y1 - y2)*(x3 - x4) - (x1 - x2)*(y3 - y4);
        
        var tx = (y1*x2 - x1*y2)*(y4 - y3)
        tx -= (x1*x2 + y1*y2)*(x3 + x4)
        tx += x3*(y2*y2 + x2*x2) + x4*(y1*y1 + x1*x1)
        
        var ty = (x1*x2 + y1*y2)*(-y4-y3)
        ty += (y1*x2 - x1*y2)*(x3-x4)
        ty += y3*(y2*y2 + x2*x2) + y4*(y1*y1 + x1*x1)
        
        return CGAffineTransformMake(a/D, -b/D, b/D, a/D, tx/D, ty/D)
    }
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值