捏缩放手势

时间:2018-06-22 15:02:28

标签: swift label pinchzoom

我编写了此代码,以允许用户捏住要放大或缩小的文本。 是否可以放置minRange和maxRange?我该怎么做。 另一个问题:双击后是否可以返回默认缩放?非常感谢。

override func viewDidLoad() {
        super.viewDidLoad()

        configureView()
        pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(pinchText(sender:)))
        tv_testoCanzone.addGestureRecognizer(pinchGesture)
        let range = NSMakeRange(tv_testoCanzone.text.count - 1, 0)
        tv_testoCanzone.scrollRangeToVisible(range)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @objc func pinchText(sender: UIPinchGestureRecognizer) {
        var pointSize = tv_testoCanzone.font?.pointSize
        pointSize = ((sender.velocity > 0) ? 1 : -1) * 1 + pointSize!;
        tv_testoCanzone.font = UIFont( name: "arial", size: (pointSize)!)
    }

1 个答案:

答案 0 :(得分:0)

可能在pointSize变量上设置上限和下限将起作用:

@objc func pinchText(sender: UIPinchGestureRecognizer) {
    var pointSize = tv_testoCanzone.font?.pointSize
    pointSize = ((sender.velocity > 0) ? 1 : -1) * 1 + pointSize!;

    let maxZoom = <some number>
    let minZoon = <some number>
    if pointSize > maxZoom {
        pointSize = maxZoom
    }
    if pointSize < minZoom {
        pointSize = minZoom
    }

    tv_testoCanzone.font = UIFont( name: "arial", size: (pointSize)!)
}

您是否考虑过使用UIScrollViewUITextView(本身就是UIScrollView)?他们为您解决了这些手势。