【问题标题】:How to implement two-finger zoom for Android in Titanium如何在 Titanium 中为 Android 实现两指缩放
【发布时间】:2012-11-02 10:53:14
【问题描述】:

如何在 Titanium 中为 Android 实现两指缩放?

我是在 Titanium 中为 iPhone 做的,因为在 iPhone 的 Titanium 中有 pinch 事件和 zoomScal 属性可用 :) 但这些不适用于 Android。 :(

此针对 iPhone 相同问题的解决方案可通过 in the Appcelerator archives 获得。

【问题讨论】:

标签: titanium titanium-mobile


【解决方案1】:

这是一篇旧帖子,但这个模块 titanium-android-pinchview 可能对某人有用

【讨论】:

    【解决方案2】:

    您必须包装 touchstarttouchmovetouchend 事件才能在 Android 中模拟此行为。此外,由于许多 Android 设备不支持多点触控,因此您需要考虑这一点。

    首先您必须正确设置滚动视图,例如,由于 Android 没有 zoomScale 属性,您需要使用矩阵来模拟它。像这样:

    var scrollView = Ti.UI.createScrollView({... Init here...});
    var contentOfScrollView = Ti.UI.createView({
        width : //..bigger than scrollview,
        height : //..bigger than scrollview,
        transform : Ti.UI.create2DMatrix()
    });
    

    首先,我会尝试跟踪视图中所有活动的触摸对象。然后,如果您有两次触摸,获取它们之间的距离并将其保存为原始缩放比例,由此您可以在用户移动时比较新的距离。如果有两个以上的触摸,这种方法就会失效,并且不是很健壮,因为它不能解释任何边缘情况,但它有时会起作用:-)

    主要问题是 Titanium 不传递触摸的 ID,或者允许您获取所有触摸,因此当您删除或添加触摸时,很难跟踪哪个触摸是哪个触摸。

    这是一个简单的代码示例,它受到我上面概述的影响,(我不会尝试跟踪哪个触摸是哪个)并且您将不得不摆弄它以使其更健壮,但我相信您可以从这里拿走:

    var touches = [];
    var zoomScale = 1.0;
    var lastDistance = 0.0;
    var wratio = contentOfScrollView.width / scrollView.width;
    var hratio = contentOfScrollView.height / scrollView.height;
    

    现在让我们添加我们的监听器。

    // Listeners
    scrollview.addEventListener('touchstart' , function(e) {
        touches.push({x : e.x, y : e.y});
    });
    scrollview.addEventListener('touchmove' , function(e) {
        if(touches.length == 2 && distance > 0) {
            // Calculate distance between touches
            var dx = touches[1].x - touches[0].x;
            var dy = touches[1].y - touches[0].y;
            var distance = Math.sqrt(dx*dx+dy*dy);
    
            // Heres where you would do your scaling based on whatever your
            // use case is.  You could try something like this, tune it yourself though 
            scrollView.zoomScale = lastDistance - distance;
    
            // Now set the matrix of the content!
            contentOfScrollView.transform.scale(zoomScale*wratio, zoomScale*hratio);
            // Save for later
            lastDistance = distance;
        } else {
            // Reset everything
            lastDistance = 0.0;
        }
    });
    scrollview.addEventListener('touchend' , function(e) {
        touches.pop();
    });
    scrollview.addEventListener('touchcancel' , function(e) {
        touches.pop();
    });
    

    这不是一个完整的、健壮的或经过错误测试的实现,但我无法为您编写所有代码,请以此为起点进行试验。祝你好运!

    【讨论】:

    • Android 不支持 touchStart、touchMove 和 touchEnd 事件,请参考:docs.appcelerator.com/titanium/2.1/#!/api/…
    • 是的。但是我确实不小心使用了camlcase,所以它支持touchstarttouchmovetouchend而不是touchStart。我编辑了答案以反映这一点。 我也可以用粗体字写。 :-)
    • 在答案中...“由于 Android 没有 zoomScale 属性,因此您需要使用矩阵来模拟它。像这样:“”
    猜你喜欢
    • 2014-12-17
    • 2012-09-07
    • 2019-08-24
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多