• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 使用 UIPanGestureRecognizer 缩放和移动 UIView

[复制链接]
菜鸟教程小白 发表于 2022-12-12 14:01:02 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

请建议帮助我?

更多详情:

我会添加更多细节。

我有 View A,在自身 View 上添加 subview 。我想当我在 View A 上绘制 panGestureRegonizer 时, View A 将跟随绘制移动。

并且在移动 View A 时会缩放。当 View 移动到屏幕的顶部/左侧/底部/右侧时, View A 将缩小,当 View 移动到屏幕中心时, View A 将放大。



Best Answer-推荐答案


假设您有 vc - ViewController 并且您的 UIView Avc 的 subview 。您可以将 UIPanGestureRecognizer 添加到 A。然后将 panGestureRegonizer 作为一个 Action 拖到你的 vc 中:

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

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

那你需要把action修改成这样:

@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 为运动提供平滑的动画效果。

这将为您提供带有平移手势的“移动”功能。

EDIT 进行缩放:

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

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()
}

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

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
}

最后,我们需要修改平移手势 Action 来改变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
    })
}

关于ios - 使用 UIPanGestureRecognizer 缩放和移动 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47200132/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap