将UIView保留在Circle Objective

编程入门 行业动态 更新时间:2024-10-27 17:17:23
本文介绍了将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

本文发布于:2023-11-13 14:08:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UIView   Circle   Objective

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!