【问题标题】:Set center of rotation [Titanium]设置旋转中心 [钛]
【发布时间】:2013-07-22 18:08:32
【问题描述】:

我有一些旋转图像的代码。但是,旋转中心不等于要旋转的图像的中心。

我已经设置了图像的中心,但我不知道如何设置旋转中心。

以下是代码:

 // image code
var image = Titanium.UI.createImageView({
    backgroundImage:'test.png',
    width: 650,
    height: 650,
    center:{x:CenterX, y:CenterY},
    align:'center'
});
//rotation code
image.addEventListener('touchstart', function(e){
    var conv = e.source.convertPointToView({x: e.x, y:e.y}, win);

    var newAngle = Math.atan2(conv.y - 500, 380 - conv.x)* -(180 / Math.PI);  
    diff = (newAngle - old);

});

image.addEventListener('touchmove', function(e){

      var conv = e.source.convertPointToView({x: e.x, y:e.y}, win);

      var newAngle = Math.atan2(conv.y - 500, 380 - conv.x)* -(180 / Math.PI);
      current = (newAngle-diff); 
      var t = Ti.UI.create2DMatrix().rotate(current);
      wheel.transform = t;

});

【问题讨论】:

    标签: image rotation titanium center


    【解决方案1】:

    您必须设置动画/视图锚点,在 iOS 上您必须设置 Ti.UI.View's anchorPoint,在 Android 上您必须设置 Ti.UI.Animation's anchorPoint 或者在您的情况下,由于您使用矩阵,只需设置 @987654323 @ 创建时(适用于 Android)。

    这是您为 iOS 所做的事情:

    /* iOS */
    var image = Titanium.UI.createImageView({
        backgroundImage:'test.png',
        width: 650,
        height: 650,
        anchorPoint: {0.5, 0.5}, // ANchor point is at the center
    });
    

    或者,在 Android 上:

    /* Android */
    // Matrix, inside your event listener
    image.addEventListener('touchmove', function(e){
    
        var conv = e.source.convertPointToView({x: e.x, y:e.y}, win);
    
        var newAngle = Math.atan2(conv.y - 500, 380 - conv.x)* -(180 / Math.PI);
        current = (newAngle-diff); 
    
        // Note that I set the anchor point here.
        var t = Ti.UI.create2DMatrix({anchorPoint: {0.5, 0.5}}).rotate(current);
        wheel.transform = t;
    });
    

    【讨论】:

    • 设置图像的中心,对吗?我希望它们的旋转中心和图像都相同?
    • 不,anchorPoint 设置图片的旋转点。将其设置为 {0.5, 0.5} 意味着图像将围绕其中心旋转。
    猜你喜欢
    • 1970-01-01
    • 2017-06-07
    • 2012-07-22
    • 2013-02-12
    • 1970-01-01
    • 2016-07-07
    • 2016-01-16
    • 2015-11-05
    • 2022-11-08
    相关资源
    最近更新 更多