使用UIPanGestureRecognizer缩放并移动UIView

时间:2017-11-09 11:01:21

标签: ios objective-c uiview uipangesturerecognizer

我有一个UIView A.我在视图A上放了一个图标并尝试使用平移手势来缩放并移动此视图A.

我尝试了很多解决方案,但我做不到。

请建议帮帮我?

更详细信息:

我会添加更多细节。

我有查看A,在自我视图上添加子。我希望当我在视图A上绘制panGestureRegonizer时,视图A将移动跟随绘制。

虽然移动视图A会缩放。当视图移动到屏幕的上/左/下/右时,视图A将缩小为较小,当视图移动到屏幕中心时,视图缩放为较大。

1 个答案:

答案 0 :(得分:3)

我们假设你有vc - ViewController而你的UIView Avc的子视图。您可以将UIPanGestureRecognizer添加到A。然后将panGestureRegonizer拖动到vc作为操作:

@IBAction func panGestureAction(_ sender: UIPanGestureRecognizer) {
        //your code here
}

sender,您可以检查操作的viewlocationstate。在某些情况下,state可能会影响您的代码,具体取决于您要实现的目标。

然后您需要将操作修改为:

@IBAction func panGestureAction(_ sender: UIPanGestureRecognizer) {
        UIView.animateKeyframes(withDuration: 0.1, delay: 0, options: UIViewKeyframeAnimationOptions.calculationModeLinear, animations: {
            let location = sender.location(in: sender.view?.superview)
            sender.view?.center = location
        })
    }

此处sender.view?.superview等于vc.view。此代码段将检测平移手势,然后移动A以使A.center与手势的位置匹配。请注意,持续时间0.1为运动提供了平滑的动画效果。

这会让你"移动"平移手势的功能。

用于缩放的

编辑

逻辑:您的坐标系(CS)包含centerxy。当用户使用平移手势时,他/她在CS中生成点序列。所以我们的任务是测量CS中心与用户之间的距离。点。当我们有最远的距离时,我们可以计算缩放视图的比例因子。

var center: CGPoint! //center of the CS
let maxSize: CGSize = CGSize.init(width: 100, height: 100) // maximum size of our scaling view
var maxLengthToCenter: CGFloat! //maximum distance from the center of the CS to the furthest point in the CS

private func prepareForScaling() {
    self.center = self.view.center //we set the center of our CS to equal the center of the VC's view
    let frame = self.view.frame
    //the furthest distance in the CS is the diagonal, and we calculate it using pythagoras theorem
    self.maxLengthToCenter = (frame.width*frame.width + frame.height*frame.height).squareRoot() 
}

然后我们需要调用我们的设置功能来为扩展功能准备好数据 - 我们可以在viewDidLoad中执行此操作:

override func viewDidLoad() {
    super.viewDidLoad()  
    self.prepareForScaling()
}

然后我们需要一个辅助函数来计算我们视图的缩放大小,用于屏幕上用户的平移手势当前位置。

private func scaledSize(for location: CGPoint) -> CGSize {
    //calculate location x,y differences from the center
    let xDifference = location.x - self.center.x
    let yDifference = location.y - self.center.y

    //calculate the scale factor - note that this factor will be between 0.0(center) and 0.5(diagonal - furthest point)    
    //It is due our measurement - from center to view's edge. Consider multiplying this factor with your custom constant.
    let scaleFactor = (xDifference*xDifference + yDifference*yDifference).squareRoot()/maxLengthToCenter
    //create scaled size with maxSize and current scale factor
    let scaledSize = CGSize.init(width: maxSize.width*(1-scaleFactor), height: maxSize.height*(1-scaleFactor))

    return scaledSize
}

最后,我们需要修改我们的平移手势动作以更改A

的大小
@IBAction func panGestureAction(_ sender: UIPanGestureRecognizer) {
    UIView.animateKeyframes(withDuration: 0.1, delay: 0, options: UIViewKeyframeAnimationOptions.calculationModeLinear, animations: {

        let location = sender.location(in: sender.view?.superview)

        sender.view?.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: self.scaledSize(for: location))

        sender.view?.center = location
    })
}