在做手势时将锚点放在屏幕中央

时间:2017-08-22 06:06:43

标签: ios swift xcode calayer

我有一个图像,可以响应捏合,旋转和平移手势。我希望图像的捏合和旋转将相对于放置在屏幕中间的锚点完成,就像使用Xcode iPhone模拟器按下选项键一样。如果图像的中心可以缩放并平移到不同的位置,如何将锚点放在屏幕中间?

这是我的缩放和旋转手势功能:

@IBAction func pinchGesture(_ gestureRecognizer: UIPinchGestureRecognizer) {
    // Move the achor point of the view's layer to the touch point
    // so that scaling the view and the layer becames simpler.
    self.adjustAnchorPoint(gestureRecognizer: gestureRecognizer)


    // Scale the view by the current scale factor.
    if(gestureRecognizer.state == .began) {
        // Reset the last scale, necessary if there are multiple objects with different scales
        lastScale = gestureRecognizer.scale
    }

    if (gestureRecognizer.state == .began || gestureRecognizer.state == .changed) {
        let currentScale = gestureRecognizer.view!.layer.value(forKeyPath:"transform.scale")! as! CGFloat
        // Constants to adjust the max/min values of zoom
        let kMaxScale:CGFloat = 15.0
        let kMinScale:CGFloat = 1.0
        var newScale = 1 -  (lastScale - gestureRecognizer.scale)
        newScale = min(newScale, kMaxScale / currentScale)
        newScale = max(newScale, kMinScale / currentScale)
        let transform = (gestureRecognizer.view?.transform)!.scaledBy(x: newScale, y: newScale);
        gestureRecognizer.view?.transform = transform
        lastScale = gestureRecognizer.scale  // Store the previous scale factor for the next pinch gesture call
        scale = currentScale // Save current scale for later use
    }
}

@IBAction func rotationGesture(_ gestureRecognizer: UIRotationGestureRecognizer) {
    // Move the achor point of the view's layer to the center of the
    // user's two fingers. This creates a more natural looking rotation.
    self.adjustAnchorPoint(gestureRecognizer: gestureRecognizer)

    // Apply the rotation to the view's transform.
    if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
        gestureRecognizer.view?.transform = (gestureRecognizer.view?.transform.rotated(by: gestureRecognizer.rotation))!
        // Set the rotation to 0 to avoid compouding the
        // rotation in the view's transform.
        angle += gestureRecognizer.rotation // Save rotation angle for later use
        gestureRecognizer.rotation = 0.0
    }
}

func adjustAnchorPoint(gestureRecognizer : UIGestureRecognizer) {
    if gestureRecognizer.state == .began {
        let view = gestureRecognizer.view
        let locationInView = gestureRecognizer.location(in: view)
        let locationInSuperview = gestureRecognizer.location(in: view?.superview)

        // Move the anchor point to the the touch point and change the position of the view
        view?.layer.anchorPoint = CGPoint(x: (locationInView.x / (view?.bounds.size.width)!), y: (locationInView.y / (view?.bounds.size.height)!))
        view?.center = locationInSuperview
    }
}

修改

我看到人们并不急于进入这个领域。让我帮助分享我过去几天取得的一些进展。

首先,我写了一个函数centerAnchorPoint,它正确地将图像的锚点放在屏幕的中心,而不管之前的锚点在哪里。但是,不得缩放或旋转图像以使其工作。

func setAnchorPoint(_ anchorPoint: CGPoint, forView view: UIView) {
    var newPoint = CGPoint(x: view.bounds.size.width * anchorPoint.x, y: view.bounds.size.height * anchorPoint.y)
    var oldPoint = CGPoint(x: view.bounds.size.width * view.layer.anchorPoint.x, y: view.bounds.size.height * view.layer.anchorPoint.y)

    newPoint = newPoint.applying(view.transform)
    oldPoint = oldPoint.applying(view.transform)

    var position = view.layer.position
    position.x -= oldPoint.x
    position.x += newPoint.x

    position.y -= oldPoint.y
    position.y += newPoint.y

    view.layer.position = position
    view.layer.anchorPoint = anchorPoint
}

func centerAnchorPoint(gestureRecognizer : UIGestureRecognizer) {
    if gestureRecognizer.state == .ended {

        view?.layer.anchorPoint = CGPoint(x: (photo.bounds.midX / (view?.bounds.size.width)!), y: (photo.bounds.midY / (view?.bounds.size.height)!))
    }
}

func centerAnchorPoint() {

    // Position of the current anchor point
    let currentPosition = photo.layer.anchorPoint

    self.setAnchorPoint(CGPoint(x: 0.5, y: 0.5), forView: photo)
    // Center of the image
    let imageCenter = CGPoint(x: photo.center.x, y: photo.center.y)
    self.setAnchorPoint(currentPosition, forView: photo)

    // Center of the screen
    let screenCenter = CGPoint(x: UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY)

    // Distance between the centers
    let distanceX = screenCenter.x - imageCenter.x
    let distanceY = screenCenter.y - imageCenter.y

    // Find new anchor point
    let newAnchorPoint = CGPoint(x: (imageCenter.x+2*distanceX)/(UIScreen.main.bounds.size.width), y: (imageCenter.y+2*distanceY)/(UIScreen.main.bounds.size.height))

    //photo.layer.anchorPoint = newAnchorPoint
    self.setAnchorPoint(newAnchorPoint, forView: photo)

    let dotPath = UIBezierPath(ovalIn: CGRect(x: photo.layer.position.x-2.5, y: photo.layer.position.y-2.5, width: 5, height: 5))
    layer.path = dotPath.cgPath
}

此处使用函数setAchorPoint将锚点设置为新位置而不移动图像。

然后我通过在其末尾插入来更新panGesture函数:

if gestureRecognizer.state == .ended {
        self.centerAnchorPoint()
    }

编辑2

好的,我会尝试简单解释上面的代码。

我在做的是:

  1. 查找照片中心与屏幕中心之间的距离
  2. 应用此公式以查找锚点的新位置:
  3. newAnchorPointX =(imageCenter.x-distanceX)/ screenWidth + distanceX / screenWidth

    然后为y位置做同样的事情。

    1. 将此点设置为新锚点,而不使用setAnchorPoint函数
    2. 移动照片

      正如我所说,如果图像没有缩放,这种方法很有效。如果是,则锚点不会停留在中心位置。

      奇怪的是,距离X或距离Y并不完全取决于比例值,所以这样的事情并不是很有效:

      newAnchorPointX =(imageCenter.x-distanceX)/ screenWidth + distanceX /(scaleValue * screenWidth)

      编辑3

      我想出了缩放比例。似乎正确的比例因子必须是:

      scaleFactor = photo.frame.size.width/photo.layer.bounds.size.width
      

      我使用了这个而不是scaleValue,而且效果非常好。

      完成平移和缩放。唯一剩下的就是轮换,但看起来它是最难的。

      我首先想到的是将旋转矩阵应用于X和Y方向的增量,如下所示:

          let incrementX = (distanceX)/(screenWidth)
          let incrementY = (distanceY)/(screenHeight)
      
          // Applying rotation matrix
          let incX = incrementX*cos(angle)+incrementY*sin(angle)
          let incY = -incrementX*sin(angle)+incrementY*cos(angle)
      
          // Find new anchor point
          let newAnchorPoint = CGPoint(x: 0.5+incX, y: 0.5+incY)
      

      然而,这不起作用。

1 个答案:

答案 0 :(得分:1)

由于这个问题主要是在编辑中回答的,所以我不想过多地重复自己。

大致上我从原始问题中发布的代码中改变了:

  1. 在捏合和旋转手势功能中删除了对adjustAnchorPoint功能的调用。
  2. 将这段代码放在平移手势功能中,以便在平移照片后锚点会更新其位置:

    如果gestureRecognizer.state == .ended {         self.centerAnchorPoint()     }

  3. 更新centerAnchorPoint功能以便轮换。

  4. 完全正常的centerAnchorPoint功能(包括旋转):

        func centerAnchorPoint() {
            // Scale factor
            photo.transform = photo.transform.rotated(by: -angle)
            let curScale = photo.frame.size.width / photo.layer.bounds.size.width
            photo.transform = photo.transform.rotated(by: angle)
    
            // Position of the current anchor point
            let currentPosition = photo.layer.anchorPoint
    
            self.setAnchorPoint(CGPoint(x: 0.5, y: 0.5), forView: photo)
            // Center of the image
            let imageCenter = CGPoint(x: photo.center.x, y: photo.center.y)
            self.setAnchorPoint(currentPosition, forView: photo)
    
            // Center of the screen
            let screenCenter = CGPoint(x: UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY)
    
            // Distance between the centers
            let distanceX = screenCenter.x - imageCenter.x
            let distanceY = screenCenter.y - imageCenter.y
    
            // Apply rotational matrix to the distances
            let distX = distanceX*cos(angle)+distanceY*sin(angle)
            let distY = -distanceX*sin(angle)+distanceY*cos(angle)
    
            let incrementX = (distX)/(curScale*UIScreen.main.bounds.size.width)
            let incrementY = (distY)/(curScale*UIScreen.main.bounds.size.height)
    
            // Find new anchor point
            let newAnchorPoint = CGPoint(x: 0.5+incrementX, y: 0.5+incrementY)
    
            self.setAnchorPoint(newAnchorPoint, forView: photo)
    }
    

    这里需要注意的关键是旋转矩阵必须应用于distanceX和distanceY。比例因子也会更新,以便在整个轮换期间保持不变。