移动UIImageView时检测屏幕边缘 [英] Detecting screen edges when moving UIImageView

查看:51
本文介绍了移动UIImageView时检测屏幕边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个UIImageView,可以在视图中移动/拖动.

I currently have a UIImageView which I can move/drag around the view.

当图像移动时如何检测屏幕边缘并阻止图像移出屏幕视图?

How does on detect for the edge of the screen when the image moves and stop the image from being able to be moved out of the screens view?

推荐答案

您可以使用(p)最远视图的坐标找到 UIImageView 的矩形

You can find the rectangle of the UIImageView in the coordinates of your (outermost) view using

CGRect r = [iv convertRect:iv.bounds toView:self.view];

这就是检查 r 是否超出范围的问题,例如像这样:

Then it's a matter of checking if r goes out of bounds, e.g. like this:

CGRect i = CGRectIntersect(r,self.view.bounds);

if ( CGRectIsNull(i) )
{
    NSLog(@"way out of bounds");
} else if ( !CGRectEqualToRect(i,r) ) {
    NSLog(@"partly out of bounds");
} else {
    NSLog(@"self.view envelopes imageview");
}

当然,这应该放在您的拖动代码中,并用适当的处理方式替换 NSLog()语句(例如,仅在最后一种情况下更新位置,或者将rect转换为查看是否需要)

Of course, this should be put in your dragging code, with the NSLog() statements replaced with appropriate handling (e.g. by only updating location in the last case, or by translating the rect back into view if needed)

这篇关于移动UIImageView时检测屏幕边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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