View Zooming
Hi,
I have a problem with Layered views zooming.
In my custom view class, I have added a root-Layer and an Image-Layer.
In my custom view class under 'mouseDragged' method I implemented the layer transformations as,
float zoomFactor = endPoint.y - startPoint.y;
startPoint = endPoint;
CATransform3D zoomOut = CATransform3DMakeScale((1/1.01),(1/1.01),(1.0/1.0));
CATransform3D zoomIn = CATransform3DMakeScale(1.01f,1.01f,1.0f);
CATransform3D zoomAffine1, zoomAffine2;
if(zoomFactor > 0)
{
if((zoomInCount==0 && zoomOutCount==0) || (zoomInCount!=0 && zoomOutCount==0))
{
zoomInCount++;
zoomAffine1 = CATransform3DConcat(zoomIn, roiLayer.transform);
zoomAffine2 = CATransform3DConcat(zoomIn, imageLayer.transform);
roiLayer.transform = zoomAffine1;
imageLayer.transform = zoomAffine2;
}
}
else
{
if((zoomInCount==0 && zoomOutCount==0) || (zoomInCount==0 && zoomOutCount!=0))
{
return;
}
if(zoomInCount!=0)
{
zoomInCount--;
zoomAffine1 = CATransform3DConcat(zoomOut, roiLayer.transform);
zoomAffine2 = CATransform3DConcat(zoomOut, imageLayer.transform);
roiLayer.transform = zoomAffine1;
imageLayer.transform = zoomAffine2;
}
}
[self setNeedsDisplay:true];
The problem what I have is that the zooming is itchy and not smooth.
Please, help me in solving the problem.
Thank you in advance.
Regards,
Sreekanth.
My instant suspicion is that you are calling this code much too frequently. Disable the code and simply log when you intend to call it - maybe you're calling this about 100 times per second. You should probably filter and only call the code when the mouse has moved more that (guess) 5 pixels since the last refresh, or .1 seconds have elapsed.
At the moment, I think you code will call setNeedsDisplay even if the mouse has moved by 1 pixel and if the refresh takes a lot of effort, a small drag could generate an avalache of refreshes.
Another performance boosting strategy is to use double buffering. In this model, you maintain an image of your graphics in memory. When the mouse moves, you BLT (block transfer) from memory to the display. The benefit of this method is to reduce the work being done on refresh. If you are panning, you have to maintain an image which is larger that this display. If you are zooming, this usually isn't so great as you have to rebuild the image at different levels of zoom.
It would be very helpful if you could create send the code and I'll take a look. Please remove the build directory and zip the rest - it should be about 50k. If you have a lot of (irrelevant) code, then construct a new project which represents the problem at its simplest. When you do this, three wonderful things happen:
1) You crystallize and clarify the puzzle for yourself
2) I engage the code at the exact point of the puzzle
3) We have shared code with which to discuss possilbe workarounds/solutions
You can post the code here (in which case anyone can download it), or email it directly to me at robin@clanmills.com
Thank you for your reply.
I tried refreshing the layers after every mouse dragging for about 5 pixels, which makes my zooming very smooth.