在Titanium中实现图片的Rotate和Pinch Gesture

在iPhone中,图片的缩放,移动,旋转以及Pinch Gesture功能在Titanium Mobile中并不支持这样Gesture。这里有人做了一个Module来实现这个功能。(只限于iOS)

Gesture-Recognizer

在view中将rotateGesture、pinchGesture设置为true后,就能处理rotate,pinch的事件了。

Gesture完成后,将触发rotateend、pinchend事件。

代码大概是下边这样:
var window = Ti.UI.createWindow({  
    orientationModes:[  
              Ti.UI.LANDSCAPE_LEFT,  
              Ti.UI.LANDSCAPE_RIGHT,  
              Ti.UI.PORTRAIT,  
              Ti.UI.UPSIDE_PORTRAIT  
             ]  
});  
  
var image = Ti.UI.createImageView({  
    image:"lena_std.jpg",  
    rotateGesture:true,  
    pinchGesture:true  
});  
var lastAngle = 0.0;  
var currentAngle = 0.0;  
  
var lastScale = 1.0;  
var currentScale = 1.0;  
  
image.addEventListener('rotate', function(e){  
    currentAngle = e.rotation / Math.PI * 180.0;  
    image.transform = Ti.UI.create2DMatrix()  
        .scale(lastScale*currentScale)  
        .rotate(lastAngle+currentAngle);  
});  
  
image.addEventListener('rotateend', function(e){  
    lastAngle = (lastAngle + currentAngle) % 360.0;  
    currentAngle = 0.0;  
});  
  
image.addEventListener('pinch', function(e){  
    currentScale = e.scale;  
    image.transform = Ti.UI.create2DMatrix()  
        .scale(lastScale*currentScale)  
        .rotate(lastAngle+currentAngle);  
});  
  
image.addEventListener('pinchend', function(e){  
    lastScale = (lastScale * currentScale);  
    currentScale = 1.0;  
});  
  
window.add(image);  
window.open(); 


效果图如下:
在Titanium中实现图片的Rotate和Pinch Gesture
在Titanium中实现图片的Rotate和Pinch Gesture
在Titanium中实现图片的Rotate和Pinch Gesture

你可能感兴趣的:(mobile,Titanium,gesture,appcelerator)