如何平移UIImageView中的图像? [英] How do I pan the image inside a UIImageView?

查看:63
本文介绍了如何平移UIImageView中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIImageView ,显示比 UIImageView 更宽更高的图像。我想使用动画来平移视图中的图像(以便平移很好和平滑)。



在我看来,我应该能够调整 UIImageView bounds.origin ,图像应该移动(因为图像应该在视图内绘制作为来源,对吗?),但是似乎不工作。 bounds.origin 更改,但图像绘制在相同的位置。



几乎可以工作的是更改 contentsRect 。但是这开始是一个单位正方形,即使图像的可视区域不是整个图像。所以我不知道我将如何检测到图像的远边缘被拉入可见区域(我需要避免,因为它通过将边缘伸展到无穷大显示,它看起来好,次标准)。



我的视图目前的 contentsGravity 设置为 kCAGravityTopLeft 通过Interface Builder,如果这有什么区别(是否导致图像移动?)。没有其他选项似乎更好。



更新:要清楚,我想移动图像视图,而

解决方案

Brad Larson指出我的道路是正确的,他建议把 UIImageView 里面 UIScrollView



code> UIImageView 里面的 UIScrollView ,并设置scrollView的 contentSize strong>和, imageView 的边界与UIImage中的图像大小相同:

  UIImage * image = imageView.image; 
imageView.bounds = CGRectMake(0,0,image.size.width,image.size.height);
scrollView.contentSize = image.size;然后,我可以动画scrollView的 contentOffset 以实现很好的平移效果:

  [UIView beginAnimations:@pancontext:nil]; 
[UIView setAnimationDuration:animationDuration];
scrollView.contentOffset = newRect.origin;
[UIView commitAnimations];

在我的特殊情况下,我正在平移到图像中的随机空间。为了找到一个正确的平移和适当的持续时间以获得一个不错的恒定速度,我使用以下:

  UIImage * image = imageView.image; 

float xNewOrigin = [TCBRandom randomIntLessThan:image.size.width - scrollView.bounds.size.width];
float yNewOrigin = [TCBRandom randomIntLessThan:image.size.height - scrollView.bounds.size.height];

CGRect oldRect = scrollView.bounds;
CGRect newRect = CGRectMake(
xNewOrigin,
yNewOrigin,
scrollView.bounds.size.width,
scrollView.bounds.size.height);

float xDistance = fabs(xNewOrigin - oldRect.origin.x);
float yDistance = fabs(yNewOrigin - oldRect.origin.y);
float hDistance = sqrtf(powf(xDistance,2)+ powf(yDistance,2));
float hDistanceInPixels = hDistance;

float animationDuration = hDistanceInPixels / speedInPixelsPerSecond;

我使用 speedInPixelsPerSecond code> 10.0f ,但其他应用程序可能需要使用不同的值。


I have a UIImageView that is displaying an image that is wider and taller than the UIImageView is. I would like to pan the image within the view using an animation (so that the pan is nice and smooth).

It seems to me that I should be able to just adjust the bounds.origin of the UIImageView, and the image should move (because the image should paint inside the view with that as its origin, right?) but that doesn't seem to work. The bounds.origin changes, but the image draws in the same location.

What almost works is to change the contentsRect of the view's layer. But this begins as a unit square, even though the viewable area of the image is not the whole image. So I'm not sure how I would detect that the far edge of the image is being pulled into the viewable area (which I need to avoid, since it displays by stretching the edge out to infinity, which looks, well, sub-par).

My view currently has its contentsGravity set to kCAGravityTopLeft via Interface Builder, if that makes a difference (Is it causing the image to move?). No other options seemed to be any better, though.

UPDATE: to be clear, I want to move the image inside the view, while keeping the view in the same spot.

解决方案

Brad Larson pointed me down the right road with his suggestion to put the UIImageView inside a UIScrollView.

In the end I put the UIImageView inside of a UIScrollView, and set the scrollView's contentSize and the imageView's bounds to be the same size as the image in the UIImage:

UIImage* image = imageView.image;
imageView.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
scrollView.contentSize = image.size;

Then, I can animate the scrollView's contentOffset to achieve a nice panning effect:

[UIView beginAnimations:@"pan" context:nil];
[UIView setAnimationDuration:animationDuration];
scrollView.contentOffset = newRect.origin;
[UIView commitAnimations];

In my particular case, I'm panning to a random space in the image. In order to find a proper rect to pan to and a proper duration to get a nice constant speed, I use the following:

UIImage* image = imageView.image;

float xNewOrigin = [TCBRandom randomIntLessThan:image.size.width - scrollView.bounds.size.width];
float yNewOrigin = [TCBRandom randomIntLessThan:image.size.height - scrollView.bounds.size.height];

CGRect oldRect = scrollView.bounds;
CGRect newRect = CGRectMake(
    xNewOrigin,
    yNewOrigin, 
    scrollView.bounds.size.width,
    scrollView.bounds.size.height);

float xDistance = fabs(xNewOrigin - oldRect.origin.x);
float yDistance = fabs(yNewOrigin - oldRect.origin.y);
float hDistance = sqrtf(powf(xDistance, 2) + powf(yDistance, 2));
float hDistanceInPixels = hDistance;

float animationDuration = hDistanceInPixels / speedInPixelsPerSecond;

I'm using a speedInPixelsPerSecond of 10.0f, but other applications might want to use a different value.

这篇关于如何平移UIImageView中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆