将UIView保留在Circle Objective-C中 [英] Keep UIView inside Circle Objective-C

查看:54
本文介绍了将UIView保留在Circle Objective-C中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何让我的选色器(UIView)停留在300px 300px的色轮内?我使用UIPanGestureRecognizer拖动色轮UIImageView周围的颜色选择器.

How can I have my Color Selector (UIView) to stay inside a color wheel that's 300px 300px? Im using the UIPanGestureRecognizer to drag the Color Selector around the color wheel UIImageView.

色轮的直径为300px.

那么如何将我悬停在色轮上的UIView在圆圈内说出来?

So how can I keep my UIView that hovers over the color wheel say inside the circle?

我们非常感谢您的帮助.

Your help is appreciated.

推荐答案

这似乎更像是一个数学问题.如果您只是想防止子视图离开圆,那么无论您在哪里移动代码,都需要插入一些逻辑.

This really seems like more of a math question. If you want to simply prevent the subview from leaving the circle then you need to insert some logic wherever you do the move code.

首先找到圆的中心.假设圆形视图是裁剪后的UIView,请通过

First find the middle of the circle. Assuming that the circle view is a clipped UIView find the center view by taking the

CGPoint center;
center.x = view.frame.origin.x + view.frame.size.width/2;
center.y = view.frame.origin.y + view.frame.size.height/2;

然后,您必须计算触摸和圆心之间的距离

Then you have to calculate that distance between the touch and the center of the circle

CGPoint touch = //location of the touch point
CGFloat distance = sqrt( pow(touch.x-center.x,2)+pow(touch.y-center.y,2) )

如果距离小于300,则可以正常移动子视图

if the distance is under 300 then you move the subview as normal

if (distance < 300) {
    //do your thing
}

如果您希望视图在离开圆环的任何位置停止移动,然后捕捉到再次输入的位置,则可以在此处停止.如果即使手指不在视图中,也要沿手指方向移动,则建议形成一个向量并将其乘以300.

if you want the view to stop moving wherever you left the circle and then snap to wherever you enter the again then you can stop here. If you want to move in the direction of your finger even when it is outside of the view then I recommend forming a vector and multiply it by 300.

CGPoint newPosition;
newPosition.x = touch.x / distance * 300;
newPosition.y = touch.y / distance * 300;

这将为您提供最靠近触摸点的圆形边缘的位置.

This will give you the position at the edge of the circle that is closest to the touch point.

这篇关于将UIView保留在Circle Objective-C中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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